Skip to content

Commit 390f223

Browse files
committed
exclude assets from embodied impact calculation
1 parent 3af0d82 commit 390f223

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

install/migration/update_1.1.1_to_1.2.0/04_update_location_zone_relation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
]);
7070

7171
$location_table = 'glpi_plugin_carbon_locations';
72+
/** @var Migration $migration */
73+
$migration->migrationOneTable($location_table);
7274
foreach ($iterator as $row) {
7375
$where = [
7476
'locations_id' => $row['locations_id'],

src/Impact/Embodied/AbstractEmbodiedImpact.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,20 @@ public function getEvaluableQuery(array $crit = [], bool $entity_restrict = true
219219
{
220220
$itemtype = static::$itemtype;
221221
$item_table = $itemtype::getTable();
222+
$glpi_item_type_table = getTableForItemType($itemtype . 'Type');
223+
$glpi_item_type_fk = getForeignKeyFieldForTable($glpi_item_type_table);
224+
$item_type_table = getTableForItemType('GlpiPlugin\\Carbon\\' . $itemtype . 'Type');
222225
$embodied_impact_table = EmbodiedImpact::getTable();
223226

224227
// $where = [];
225228
// if (!$recalculate) {
226229
// $where = [EmbodiedImpact::getTableField('id') => null];
227230
// }
228231

232+
$crit[] = [
233+
$item_type_table . '.is_ignore' => false,
234+
];
235+
229236
$request = [
230237
'SELECT' => [
231238
$itemtype::getTableField('id'),
@@ -243,6 +250,14 @@ public function getEvaluableQuery(array $crit = [], bool $entity_restrict = true
243250
],
244251
],
245252
],
253+
$item_type_table => [
254+
[
255+
'FKEY' => [
256+
$item_type_table => $glpi_item_type_fk,
257+
$item_table => $glpi_item_type_fk,
258+
],
259+
],
260+
],
246261
],
247262
'WHERE' => $crit,
248263
];
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\Impact\Engine\Boavizta\Tests;
34+
35+
use Computer;
36+
use ComputerType as GlpiComputerType;
37+
use DBmysql;
38+
use GlpiPlugin\Carbon\ComputerType;
39+
use GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Computer as BoaviztaComputer;
40+
use GlpiPlugin\Carbon\Tests\DbTestCase;
41+
42+
class ComputerTest extends DbTestCase
43+
{
44+
public function testGetEvaluableQuery()
45+
{
46+
/** @var DBmysql $DB */
47+
global $DB;
48+
49+
$glpi_computer_type = $this->createItem(GlpiComputerType::class);
50+
$computer_type = $this->createItem(ComputerType::class, [
51+
'computertypes_id' => $glpi_computer_type->getID()
52+
]);
53+
$computer = $this->createItem(Computer::class, [
54+
'computertypes_id' => $glpi_computer_type->getID()
55+
]);
56+
57+
$instance = new BoaviztaComputer();
58+
$request = $instance->getEvaluableQuery([
59+
'glpi_computers.id' => $computer->getID(),
60+
]);
61+
$this->assertArrayHasKey('SELECT', $request);
62+
$this->assertArrayHasKey('FROM', $request);
63+
$this->assertArrayHasKey('LEFT JOIN', $request);
64+
$this->assertArrayHasKey('WHERE', $request);
65+
$iterator = $DB->request($request);
66+
$this->assertEquals(1, $iterator->count());
67+
68+
$glpi_computer_type = $this->createItem(GlpiComputerType::class);
69+
$computer_type = $this->createItem(ComputerType::class, [
70+
'computertypes_id' => $glpi_computer_type->getID(),
71+
'is_ignore' => 1,
72+
]);
73+
$computer = $this->createItem(Computer::class, [
74+
'computertypes_id' => $glpi_computer_type->getID()
75+
]);
76+
77+
$instance = new BoaviztaComputer();
78+
$request = $instance->getEvaluableQuery([
79+
'glpi_computers.id' => $computer->getID(),
80+
]);
81+
$this->assertArrayHasKey('SELECT', $request);
82+
$this->assertArrayHasKey('FROM', $request);
83+
$this->assertArrayHasKey('LEFT JOIN', $request);
84+
$this->assertArrayHasKey('WHERE', $request);
85+
$iterator = $DB->request($request);
86+
$this->assertEquals(0, $iterator->count());
87+
}
88+
89+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\Impact\Engine\Boavizta\Tests;
34+
35+
use Monitor;
36+
use MonitorType as GlpiMonitorType;
37+
use DBmysql;
38+
use GlpiPlugin\Carbon\MonitorType;
39+
use GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Monitor as BoaviztaMonitor;
40+
use GlpiPlugin\Carbon\Tests\DbTestCase;
41+
42+
class MonitorTest extends DbTestCase
43+
{
44+
public function testGetEvaluableQuery()
45+
{
46+
/** @var DBmysql $DB */
47+
global $DB;
48+
49+
$glpi_monitor_type = $this->createItem(GlpiMonitorType::class);
50+
$monitor_type = $this->createItem(MonitorType::class, [
51+
'monitortypes_id' => $glpi_monitor_type->getID()
52+
]);
53+
$monitor = $this->createItem(Monitor::class, [
54+
'monitortypes_id' => $glpi_monitor_type->getID()
55+
]);
56+
57+
$instance = new BoaviztaMonitor();
58+
$request = $instance->getEvaluableQuery([
59+
'glpi_monitors.id' => $monitor->getID(),
60+
]);
61+
$this->assertArrayHasKey('SELECT', $request);
62+
$this->assertArrayHasKey('FROM', $request);
63+
$this->assertArrayHasKey('LEFT JOIN', $request);
64+
$this->assertArrayHasKey('WHERE', $request);
65+
$iterator = $DB->request($request);
66+
$this->assertEquals(1, $iterator->count());
67+
68+
$glpi_monitor_type = $this->createItem(GlpiMonitorType::class);
69+
$monitor_type = $this->createItem(MonitorType::class, [
70+
'monitortypes_id' => $glpi_monitor_type->getID(),
71+
'is_ignore' => 1,
72+
]);
73+
$monitor = $this->createItem(Monitor::class, [
74+
'monitortypes_id' => $glpi_monitor_type->getID()
75+
]);
76+
77+
$instance = new BoaviztaMonitor();
78+
$request = $instance->getEvaluableQuery([
79+
'glpi_monitors.id' => $monitor->getID(),
80+
]);
81+
$this->assertArrayHasKey('SELECT', $request);
82+
$this->assertArrayHasKey('FROM', $request);
83+
$this->assertArrayHasKey('LEFT JOIN', $request);
84+
$this->assertArrayHasKey('WHERE', $request);
85+
$iterator = $DB->request($request);
86+
$this->assertEquals(0, $iterator->count());
87+
}
88+
89+
}

0 commit comments

Comments
 (0)