From b14fbab8ed9972116da25f74292d971f8cf7c2e8 Mon Sep 17 00:00:00 2001 From: "Romain B." <8530352+Rom1-B@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:50:59 +0200 Subject: [PATCH] Fix(Contract): Expiration date (#17725) * Fix(Contract): Expiration date * add tooltip --- phpunit/functional/ContractTest.php | 28 +++++++++++++++---- src/Contract.php | 6 ++-- src/Infocom.php | 11 +++++--- templates/pages/management/contract.html.twig | 26 +++++++++++------ 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/phpunit/functional/ContractTest.php b/phpunit/functional/ContractTest.php index 244e596f46d..07cddc4be31 100644 --- a/phpunit/functional/ContractTest.php +++ b/phpunit/functional/ContractTest.php @@ -110,7 +110,8 @@ public static function getSpecificValueToDisplayProvider() 'values' => [ 'begin_date' => '2020-01-01', 'duration' => 6, - 'renewal' => \Contract::RENEWAL_NEVER + 'renewal' => \Contract::RENEWAL_NEVER, + 'periodicity' => 0 ], 'expected' => "2020-07-01" ], @@ -119,7 +120,8 @@ public static function getSpecificValueToDisplayProvider() 'values' => [ 'begin_date' => '2020-01-01', 'duration' => 6, - 'renewal' => \Contract::RENEWAL_TACIT + 'renewal' => \Contract::RENEWAL_TACIT, + 'periodicity' => 0 ], 'expected' => "2024-07-01" ], @@ -128,7 +130,8 @@ public static function getSpecificValueToDisplayProvider() 'values' => [ 'begin_date' => '2020-01-01', 'duration' => 6, - 'renewal' => \Contract::RENEWAL_EXPRESS + 'renewal' => \Contract::RENEWAL_EXPRESS, + 'periodicity' => 0 ], 'expected' => "2020-07-01" ], @@ -137,7 +140,8 @@ public static function getSpecificValueToDisplayProvider() 'values' => [ 'begin_date' => '2025-01-01', 'duration' => 6, - 'renewal' => \Contract::RENEWAL_NEVER + 'renewal' => \Contract::RENEWAL_NEVER, + 'periodicity' => 0 ], 'expected' => '2025-07-01' ], @@ -146,7 +150,8 @@ public static function getSpecificValueToDisplayProvider() 'values' => [ 'begin_date' => '2025-01-01', 'duration' => 6, - 'renewal' => \Contract::RENEWAL_TACIT + 'renewal' => \Contract::RENEWAL_TACIT, + 'periodicity' => 0 ], 'expected' => '2025-07-01' ], @@ -155,10 +160,21 @@ public static function getSpecificValueToDisplayProvider() 'values' => [ 'begin_date' => '2025-01-01', 'duration' => 6, - 'renewal' => \Contract::RENEWAL_EXPRESS + 'renewal' => \Contract::RENEWAL_EXPRESS, + 'periodicity' => 0 ], 'expected' => '2025-07-01' ], + [ + 'field' => '_virtual_expiration', + 'values' => [ + 'begin_date' => '2019-01-01', + 'duration' => 60, + 'renewal' => \Contract::RENEWAL_TACIT, + 'periodicity' => 12 + ], + 'expected' => '2025-01-01' + ], ]; } diff --git a/src/Contract.php b/src/Contract.php index 9b238580872..032feeca8d5 100644 --- a/src/Contract.php +++ b/src/Contract.php @@ -471,7 +471,8 @@ public static function getSpecificValueToDisplay($field, $values, array $options $values['duration'], 0, true, - ($values['renewal'] == self::RENEWAL_TACIT) + ($values['renewal'] == self::RENEWAL_TACIT), + $values['periodicity'] ); } return parent::getSpecificValueToDisplay($field, $values, $options); @@ -667,7 +668,8 @@ public function rawSearchOptions() 'additionalfields' => [ 'begin_date', 'duration', - 'renewal' + 'renewal', + 'periodicity' ], 'name' => __('Expiration'), 'datatype' => 'specific', diff --git a/src/Infocom.php b/src/Infocom.php index 23514692ad2..0b9aef302b6 100644 --- a/src/Infocom.php +++ b/src/Infocom.php @@ -1763,13 +1763,14 @@ public function showDebug() * @param integer $deletenotice period in months of notice (default 0) * @param boolean $color if show expire date in red color (false by default) * @param boolean $auto_renew + * @param integer $periodicity renewal periodicity in month if different from addwarranty * * @return string expiration date **/ - public static function getWarrantyExpir($from, $addwarranty, $deletenotice = 0, $color = false, $auto_renew = false) + public static function getWarrantyExpir($from, $addwarranty, $deletenotice = 0, $color = false, $auto_renew = false, $periodicity = 0) { - // Life warranty + // Life warranty if ( ($addwarranty == -1) && ($deletenotice == 0) @@ -1783,11 +1784,13 @@ public static function getWarrantyExpir($from, $addwarranty, $deletenotice = 0, $timestamp = strtotime("$from+$addwarranty month -$deletenotice month"); - if ($auto_renew && $addwarranty > 0) { + $periodicity = ($periodicity > 0) ? $periodicity : $addwarranty; + + if ($auto_renew && $periodicity > 0) { while ($timestamp < strtotime($_SESSION['glpi_currenttime'])) { $datetime = new DateTime(); $datetime->setTimestamp($timestamp); - $timestamp = strtotime($datetime->format("Y-m-d H:i:s") . "+$addwarranty month"); + $timestamp = strtotime($datetime->format("Y-m-d H:i:s") . "+$periodicity month"); } } diff --git a/templates/pages/management/contract.html.twig b/templates/pages/management/contract.html.twig index 221bcae357a..c74dc96bc86 100644 --- a/templates/pages/management/contract.html.twig +++ b/templates/pages/management/contract.html.twig @@ -44,15 +44,22 @@ ) }} {% if item.fields['begin_date'] is not empty and item.fields['duration'] is not empty %} + {% set end_title = __('End date') %} + {% if item.fields['renewal'] == constant('Contract::RENEWAL_TACIT') %} + {% set end_title = __('Next renewal date') %} + {% endif %} {% set warranty_expiration %} - - {{ call('Infocom::getWarrantyExpir', [ - item.fields['begin_date'], - item.fields['duration'], - 0, - true, - item.fields['renewal'] == constant('Contract::RENEWAL_TACIT') - ])|raw }} + + + {{ call('Infocom::getWarrantyExpir', [ + item.fields['begin_date'], + item.fields['duration'], + 0, + true, + item.fields['renewal'] == constant('Contract::RENEWAL_TACIT'), + item.fields['periodicity'] + ])|raw }} + {% endset %} {% endif %} {{ fields.dropdownNumberField('duration', item.fields['duration'], __('Initial contract period'), { @@ -72,7 +79,8 @@ item.fields['begin_date'], item.fields['duration'], item.fields['notice'], - true + true, + item.fields['periodicity'] ])|raw }} {% endset %} {% endif %}