Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/

/** @var Migration $migration */
$table = 'glpi_plugin_carbon_usageinfos';
$migration->dropField($table, 'planned_lifespan');
1 change: 0 additions & 1 deletion install/mysql/plugin_carbon_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_usageinfos` (
`itemtype` varchar(255) DEFAULT NULL,
`items_id` int unsigned NOT NULL DEFAULT '0',
`plugin_carbon_computerusageprofiles_id` int unsigned NOT NULL DEFAULT '0',
`planned_lifespan` int unsigned NOT NULL DEFAULT '0' COMMENT '(unit months)',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`itemtype`, `items_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand Down
24 changes: 3 additions & 21 deletions src/UsageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use CommonDBTM;
use CommonGLPI;
use Computer as GlpiComputer;
use DateInterval;
use DateTime;
use Glpi\Application\View\TemplateRenderer;
use GlpiPlugin\Carbon\Dashboard\Provider;
Expand Down Expand Up @@ -166,7 +165,6 @@ public function showForItem($ID, $withtemplate = '')
'items_id' => $this->fields['items_id'],
]);

$this->fields['planned_lifespan'] ??= Toolbox::getInfocomLifespanInMonth($infocom);
TemplateRenderer::getInstance()->display('@carbon/usageinfo.html.twig', [
'params' => $options,
'item' => $this,
Expand Down Expand Up @@ -280,11 +278,9 @@ public static function showCharts(CommonDBTM $asset)
}

/**
* Get the lifespan of an asset from its infocom or usage info, in hours
* Get the lifespan of an asset from its infocom, in hours
*
* determine an interval between the best date for interval start and the best date for interval end
* interval start comes from, by precedence: date_creation of the asset, then from infocom: use_date, delivery_date, buy_date
* interval end comes from, by precedence: decommission_date from infocom, then planned_lifespan from usage info
* determine an interval between the best date for interval start and the decommission_date
*
* @param CommonDBTM $item
* @return int|null
Expand Down Expand Up @@ -315,25 +311,11 @@ public static function getLifespanInHours(CommonDBTM $item): ?int
}
}

if ($start_date === null) {
if ($start_date === null || $end_date === null) {
//Failed to find any date to use as start date, then we can't calculate a lifespan, return null
return null;
}

if ($end_date === null) {
// Try to get end date from usage info and start_date
$usage_info = new UsageInfo();
$usage_info->getFromDBByCrit([
'itemtype' => get_class($item),
'items_id' => $item->getID(),
]);
if ($usage_info->isNewItem() || $usage_info->fields['planned_lifespan'] <= 0) {
// Failed to find a planned lifespan, then we can't calculate a lifespan, return null
return null;
}
$end_date = (new DateTime($start_date))->add(new DateInterval('P' . $usage_info->fields['planned_lifespan'] . 'M'));
}

$interval = $end_date->diff(new DateTime($start_date));
$lifespan_in_hours = $interval->days * 24 + $interval->h;
return $lifespan_in_hours;
Expand Down
15 changes: 0 additions & 15 deletions templates/usageinfo.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,4 @@
item.fields['plugin_carbon_computerusageprofiles_id'],
_n('Usage profile', 'Usage profiles', 1, 'carbon')) }}
{% endif %}
{% set readonly = false %}
{% if (infocom.fields.decommission_date ?? null) is not null %}
{% set label = __('lifespan (in months)', 'carbon') %}
{% set readonly = true %}
{% else %}
{% set label = __('planned lifespan (in months)', 'carbon') %}
{% endif %}
{{ fields.numberField('planned_lifespan',
item.fields['planned_lifespan'],
label,
{
display_emptychoice: true,
min: 0,
readonly: readonly,
}) }}
{% endblock %}
22 changes: 0 additions & 22 deletions tests/units/UsageInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,26 +339,4 @@ public function test_getLifespanInHours_returns_hours_when_date_creation()
$result = $usage_info->getLifespanInHours($glpi_computer);
$this->assertSame(35867, $result);
}

public function test_getLifespanInHours_returns_hours_when_no_decommission_date_but_has_planned_lifetime()
{
$glpi_computer = $this->createItem(GlpiComputer::class);
$infocom = $this->createItem(Infocom::class, [
'itemtype' => get_class($glpi_computer),
'items_id' => $glpi_computer->getID(),
'delivery_date' => null,
'use_date' => '2022-01-01',
'buy_date' => null,
'decommission_date' => null,
]);
$usage_info = $this->createItem(UsageInfo::class, [
'itemtype' => get_class($glpi_computer),
'items_id' => $glpi_computer->getID(),
'planned_lifespan' => 60, // 5 years = 60 months
]);

$usage_info = new UsageInfo();
$result = $usage_info->getLifespanInHours($glpi_computer);
$this->assertSame(43824, $result);
}
}