diff --git a/install/migration/update_1.1.1_to_1.2.0/07_drop_planned_lifespan.php b/install/migration/update_1.1.1_to_1.2.0/07_drop_planned_lifespan.php new file mode 100644 index 00000000..bc8265d6 --- /dev/null +++ b/install/migration/update_1.1.1_to_1.2.0/07_drop_planned_lifespan.php @@ -0,0 +1,35 @@ +. + * + * ------------------------------------------------------------------------- + */ + +/** @var Migration $migration */ +$table = 'glpi_plugin_carbon_usageinfos'; +$migration->dropField($table, 'planned_lifespan'); diff --git a/install/mysql/plugin_carbon_empty.sql b/install/mysql/plugin_carbon_empty.sql index c699f233..48a78f46 100644 --- a/install/mysql/plugin_carbon_empty.sql +++ b/install/mysql/plugin_carbon_empty.sql @@ -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; diff --git a/src/UsageInfo.php b/src/UsageInfo.php index f9a24207..e8c06ce5 100644 --- a/src/UsageInfo.php +++ b/src/UsageInfo.php @@ -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; @@ -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, @@ -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 @@ -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; diff --git a/templates/usageinfo.html.twig b/templates/usageinfo.html.twig index 1b74b47e..d9de1f57 100644 --- a/templates/usageinfo.html.twig +++ b/templates/usageinfo.html.twig @@ -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 %} diff --git a/tests/units/UsageInfoTest.php b/tests/units/UsageInfoTest.php index 3919afbb..46ed3aec 100644 --- a/tests/units/UsageInfoTest.php +++ b/tests/units/UsageInfoTest.php @@ -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); - } }