Skip to content

Commit

Permalink
use 'computation' instead of 'workaround'
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed May 6, 2024
1 parent 985862c commit 57f0a61
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
17 changes: 13 additions & 4 deletions src/ConsumableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public function defineTabs($options = [])

public function rawSearchOptions()
{
/** @var \DBmysql $DB */
global $DB;

$tab = parent::rawSearchOptions();

$tab[] = [
Expand Down Expand Up @@ -215,27 +218,33 @@ 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[] = [
'id' => '19',
'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());
Expand Down
26 changes: 0 additions & 26 deletions src/Search/Provider/SQLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 57f0a61

Please sign in to comment.