From 57f0a61a3fc7316f603de06b9af2240171e7768a Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Mon, 6 May 2024 15:44:02 +0200 Subject: [PATCH] use 'computation' instead of 'workaround' --- src/ConsumableItem.php | 17 +++++++++++++---- src/Search/Provider/SQLProvider.php | 26 -------------------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/ConsumableItem.php b/src/ConsumableItem.php index 449913e6c00..51481c26698 100644 --- a/src/ConsumableItem.php +++ b/src/ConsumableItem.php @@ -154,6 +154,9 @@ public function defineTabs($options = []) public function rawSearchOptions() { + /** @var \DBmysql $DB */ + global $DB; + $tab = parent::rawSearchOptions(); $tab[] = [ @@ -215,13 +218,16 @@ public function rawSearchOptions() 'table' => 'glpi_consumables', 'field' => 'id', 'name' => __('Number of used consumables'), - 'datatype' => 'count', + 'datatype' => 'number', 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', - ] + ], + 'computation' => new QueryExpression( + expression: QueryFunction::sum(new QueryExpression("CASE WHEN " . $DB::quoteName('TABLE.date_out') . " IS NULL THEN 1 ELSE 0 END")) + ) ]; $tab[] = [ @@ -229,13 +235,16 @@ public function rawSearchOptions() 'table' => 'glpi_consumables', 'field' => 'id', 'name' => __('Number of new consumables'), - 'datatype' => 'count', + 'datatype' => 'number', 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', - ] + ], + 'computation' => new QueryExpression( + expression: QueryFunction::sum(new QueryExpression("CASE WHEN " . $DB::quoteName('TABLE.date_out') . " IS NOT NULL THEN 1 ELSE 0 END")) + ) ]; $tab = array_merge($tab, Location::rawSearchOptionsToAdd()); diff --git a/src/Search/Provider/SQLProvider.php b/src/Search/Provider/SQLProvider.php index 24d635768ea..d074e9ac7b7 100644 --- a/src/Search/Provider/SQLProvider.php +++ b/src/Search/Provider/SQLProvider.php @@ -462,32 +462,6 @@ public static function getSelectCriteria(string $itemtype, int $ID, bool $meta = return array_merge($SELECT, $ADDITONALFIELDS); } break; - case "glpi_consumables.id": - if ( - $itemtype == ConsumableItem::class - && ($ID == 17 || $ID = 19) - && $opt_arrays[$ID]["datatype"] == 'count' - ) { - // forces the LEFT JOIN filter directly into the SELECT statement to optimise data loading - if ($ID == 19) { - $SELECT = [ - new QueryExpression( - QueryFunction::sum(new QueryExpression("CASE WHEN $table$addtable.date_out IS NULL THEN 1 ELSE 0 END")), - alias: $NAME - ) - ]; - return array_merge($SELECT, $ADDITONALFIELDS); - } else { - $SELECT = [ - new QueryExpression( - QueryFunction::sum(new QueryExpression("CASE WHEN $table$addtable.date_out IS NOT NULL THEN 1 ELSE 0 END")), - alias: $NAME - ) - ]; - return array_merge($SELECT, $ADDITONALFIELDS); - } - } - break; default: break; }