Skip to content

Commit 518737a

Browse files
committed
fix(Impact\Embodied\AbstractEmbodiedImpact): use manual input data if available, instead of Boavizta
1 parent 91f430d commit 518737a

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/Impact/Embodied/AbstractEmbodiedImpact.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
use DbUtils;
3838
use CommonDBTM;
3939
use DBmysqlIterator;
40+
use GlpiPlugin\Carbon\AbstractModel;
4041
use GlpiPlugin\Carbon\DataTracking\AbstractTracked;
42+
use GlpiPlugin\Carbon\DataTracking\TrackedFloat;
4143
use GlpiPlugin\Carbon\EmbodiedImpact;
4244
use GlpiPlugin\Carbon\Impact\Type;
4345
use Toolbox as GlpiToolbox;
@@ -181,15 +183,30 @@ public function evaluateItem(int $id): bool
181183
if ($item === false) {
182184
return false;
183185
}
184-
try {
185-
$impacts = $this->doEvaluation($item);
186-
} catch (\RuntimeException $e) {
187-
return false;
188-
}
189186

190-
if ($impacts === null) {
191-
// Nothing calculated
192-
return false;
187+
// Check the asset does not has embodied impact data in its model
188+
$glpi_model_class = $itemtype . 'Model';
189+
$glpi_model_class_fk = getForeignKeyFieldForItemType($glpi_model_class);
190+
/**
191+
* @var class-string<AbstractModel> $model_class
192+
*/
193+
$model_class = 'GlpiPlugin\\Carbon\\' . $glpi_model_class;
194+
$glpi_model_id = $item->fields[$glpi_model_class_fk];
195+
$model = new $model_class;
196+
197+
if ($model->getFromDBByCrit([$glpi_model_class_fk => $glpi_model_id]) !== false) {
198+
$impacts = $this->getModelImpacts($model);
199+
} else {
200+
try {
201+
$impacts = $this->doEvaluation($item);
202+
} catch (\RuntimeException $e) {
203+
return false;
204+
}
205+
206+
if ($impacts === null) {
207+
// Nothing calculated
208+
return false;
209+
}
193210
}
194211

195212
// Find an existing row, if any
@@ -312,4 +329,14 @@ public function resetForItem(int $items_id): bool
312329
'items_id' => $items_id
313330
]);
314331
}
332+
333+
protected function getModelImpacts(CommonDBTM $model): array
334+
{
335+
$impacts = [];
336+
$types = Type::getImpactTypes();
337+
foreach ($types as $key => $type) {
338+
$impacts[$key] = new TrackedFloat($model->fields[$type], null, $model->fields[$type . '_quality']);
339+
};
340+
return $impacts;
341+
}
315342
}

0 commit comments

Comments
 (0)