Skip to content

Commit

Permalink
Add cartidge/consumable restock target value (#9687)
Browse files Browse the repository at this point in the history
* Add restock target value

* Fix lint

* Move field to new templates

* Fix twig headers
  • Loading branch information
cconard96 authored Nov 23, 2021
1 parent 2434a4d commit 33ff728
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 17 deletions.
44 changes: 44 additions & 0 deletions inc/cartridge.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,50 @@ static function getUnusedNumber($tID) {
return $result['cpt'];
}

/**
* The desired stock level
*
* This is used when the alarm threshold is reached to know how many to order.
* @param integer $tID Cartridge item ID
* @return integer
*/
static function getStockTarget(int $tID): int {
global $DB;

$it = $DB->request([
'COUNT' => 'stock_target',
'FROM' => CartridgeItem::getTable(),
'WHERE' => [
'id' => $tID
]
]);
if ($it->count()) {
return $it->next()['stock_target'];
}
return 0;
}

/**
* The lower threshold for the stock amount before an alarm is triggered
*
* @param integer $tID Cartridge item ID
* @return integer
*/
static function getAlarmThreshold(int $tID): int {
global $DB;

$it = $DB->request([
'COUNT' => 'alarm_threshold',
'FROM' => CartridgeItem::getTable(),
'WHERE' => [
'id' => $tID
]
]);
if ($it->count()) {
return $it->next()['stock_target'];
}
return 0;
}

/**
* Get the translated value for the status of a cartridge based on the use and out date (if any).
Expand Down
9 changes: 9 additions & 0 deletions inc/cartridgeitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
die("Sorry. You can't access this file directly");
}

use Glpi\Application\View\TemplateRenderer;
use Glpi\Features\AssetImage;

/**
Expand Down Expand Up @@ -559,6 +560,14 @@ function showDebug() {
NotificationEvent::debugEvent($this, $options);
}

function showForm($ID, array $options = []) {
$this->initForm($ID, $options);
TemplateRenderer::getInstance()->display('pages/assets/cartridgeitem.html.twig', [
'item' => $this,
'params' => $options,
]);
return true;
}

static function getIcon() {
return Cartridge::getIcon();
Expand Down
44 changes: 44 additions & 0 deletions inc/consumable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,50 @@ static function getUnusedNumber($tID) {
return(int) $result['cpt'];
}

/**
* The desired stock level
*
* This is used when the alarm threshold is reached to know how many to order.
* @param integer $tID Consumable item ID
* @return integer
*/
static function getStockTarget(int $tID): int {
global $DB;

$it = $DB->request([
'COUNT' => 'stock_target',
'FROM' => ConsumableItem::getTable(),
'WHERE' => [
'id' => $tID
]
]);
if ($it->count()) {
return $it->next()['stock_target'];
}
return 0;
}

/**
* The lower threshold for the stock amount before an alarm is triggered
*
* @param integer $tID Consumable item ID
* @return integer
*/
static function getAlarmThreshold(int $tID): int {
global $DB;

$it = $DB->request([
'COUNT' => 'alarm_threshold',
'FROM' => ConsumableItem::getTable(),
'WHERE' => [
'id' => $tID
]
]);
if ($it->count()) {
return $it->next()['stock_target'];
}
return 0;
}

/**
* Get the consumable count HTML array for a defined consumable type
Expand Down
9 changes: 9 additions & 0 deletions inc/consumableitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
die("Sorry. You can't access this file directly");
}

use Glpi\Application\View\TemplateRenderer;
use Glpi\Features\AssetImage;

//! ConsumableItem Class
Expand Down Expand Up @@ -428,6 +429,14 @@ function canUpdateItem() {
return true;
}

function showForm($ID, array $options = []) {
$this->initForm($ID, $options);
TemplateRenderer::getInstance()->display('pages/assets/consumableitem.html.twig', [
'item' => $this,
'params' => $options,
]);
return true;
}

static function getIcon() {
return Consumable::getIcon();
Expand Down
28 changes: 21 additions & 7 deletions inc/notificationtargetcartridgeitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ function addDataForTemplate($event, $options = []) {
$this->data['##cartridge.action##'] = $events[$event];

foreach ($options['items'] as $id => $cartridge) {
$remaining_stock = Cartridge::getUnusedNumber($id);
$target_stock = Cartridge::getStockTarget($id);
if ($target_stock <= 0) {
$alarm_threshold = Cartridge::getAlarmThreshold($id);
$target_stock = $alarm_threshold + 1;
}
$to_order = $target_stock - $remaining_stock;
$tmp = [];
$tmp['##cartridge.item##'] = $cartridge['name'];
$tmp['##cartridge.reference##'] = $cartridge['ref'];
$tmp['##cartridge.remaining##'] = Cartridge::getUnusedNumber($id);
$tmp['##cartridge.remaining##'] = $remaining_stock;
$tmp['##cartridge.stock_target##'] = $target_stock;
$tmp['##cartridge.to_order##'] = $to_order;
$tmp['##cartridge.url##'] = $this->formatURL($options['additionnaloption']['usertype'],
"CartridgeItem_".$id);

$this->data['cartridges'][] = $tmp;
}

Expand All @@ -77,12 +87,16 @@ function addDataForTemplate($event, $options = []) {

function getTags() {

$tags = ['cartridge.action' => _n('Event', 'Events', 1),
'cartridge.reference' => __('Reference'),
'cartridge.item' => CartridgeItem::getTypeName(1),
'cartridge.remaining' => __('Remaining'),
'cartridge.url' => __('URL'),
'cartridge.entity' => Entity::getTypeName(1)];
$tags = [
'cartridge.action' => _n('Event', 'Events', 1),
'cartridge.reference' => __('Reference'),
'cartridge.item' => CartridgeItem::getTypeName(1),
'cartridge.remaining' => __('Remaining'),
'cartridge.stock_target' => __('Stock target'),
'cartridge.to_order' => __('To order'),
'cartridge.url' => __('URL'),
'cartridge.entity' => Entity::getTypeName(1)
];

foreach ($tags as $tag => $label) {
$this->addTagToList(['tag' => $tag,
Expand Down
25 changes: 19 additions & 6 deletions inc/notificationtargetconsumableitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,19 @@ function addDataForTemplate($event, $options = []) {
$this->data['##consumable.action##'] = $events[$event];

foreach ($options['items'] as $id => $consumable) {
$remaining_stock = Consumable::getUnusedNumber($id);
$target_stock = Consumable::getStockTarget($id);
if ($target_stock <= 0) {
$alarm_threshold = Consumable::getAlarmThreshold($id);
$target_stock = $alarm_threshold + 1;
}
$to_order = $target_stock - $remaining_stock;
$tmp = [];
$tmp['##consumable.item##'] = $consumable['name'];
$tmp['##consumable.reference##'] = $consumable['ref'];
$tmp['##consumable.remaining##'] = Consumable::getUnusedNumber($id);
$tmp['##consumable.remaining##'] = $remaining_stock;
$tmp['##consumable.stock_target##'] = $target_stock;
$tmp['##consumable.to_order##'] = $to_order;
$tmp['##consumable.url##'] = $this->formatURL($options['additionnaloption']['usertype'],
"ConsumableItem_".$id);
$this->data['consumables'][] = $tmp;
Expand All @@ -78,11 +87,15 @@ function addDataForTemplate($event, $options = []) {

function getTags() {

$tags = ['consumable.action' => _n('Event', 'Events', 1),
'consumable.reference' => __('Reference'),
'consumable.item' => ConsumableItem::getTypeName(1),
'consumable.remaining' => __('Remaining'),
'consumable.entity' => Entity::getTypeName(1)];
$tags = [
'consumable.action' => _n('Event', 'Events', 1),
'consumable.reference' => __('Reference'),
'consumable.item' => ConsumableItem::getTypeName(1),
'consumable.remaining' => __('Remaining'),
'consumable.stock_target' => __('Stock target'),
'consumable.to_order' => __('To order'),
'consumable.entity' => Entity::getTypeName(1)
];

foreach ($tags as $tag => $label) {
$this->addTagToList(['tag' => $tag,
Expand Down
10 changes: 10 additions & 0 deletions install/empty_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4285,6 +4285,8 @@
##lang.consumable.reference## : ##consumable.reference##
##lang.consumable.remaining## : ##consumable.remaining##
##lang.consumable.stock_target## : ##consumable.stock_target##
##lang.consumable.to_order## : ##consumable.to_order##
##consumable.url##
Expand All @@ -4295,6 +4297,8 @@
&lt;br /&gt;##lang.consumable.item## : ##consumable.item##&lt;br /&gt;
&lt;br /&gt;##lang.consumable.reference## : ##consumable.reference##&lt;br /&gt;
##lang.consumable.remaining## : ##consumable.remaining##&lt;br /&gt;
##lang.consumable.stock_target## : ##consumable.stock_target##&lt;br /&gt;
##lang.consumable.to_order## : ##consumable.to_order##&lt;br /&gt;
&lt;a href="##consumable.url##"&gt; ##consumable.url##&lt;/a&gt;&lt;br /&gt;
##ENDFOREACHconsumables##&lt;/p&gt;',
], [
Expand All @@ -4312,6 +4316,8 @@
##lang.cartridge.reference## : ##cartridge.reference##
##lang.cartridge.remaining## : ##cartridge.remaining##
##lang.cartridge.stock_target## : ##cartridge.stock_target##
##lang.cartridge.to_order## : ##cartridge.to_order##
##cartridge.url##
##ENDFOREACHcartridges##',
Expand All @@ -4323,6 +4329,10 @@
##cartridge.reference##&lt;br /&gt;
##lang.cartridge.remaining## :
##cartridge.remaining##&lt;br /&gt;
##lang.cartridge.stock_target## :
##cartridge.stock_target##&lt;br /&gt;
##lang.cartridge.to_order## :
##cartridge.to_order##&lt;br /&gt;
&lt;a href="##cartridge.url##"&gt;
##cartridge.url##&lt;/a&gt;&lt;br /&gt;
##ENDFOREACHcartridges##&lt;/p&gt;',
Expand Down
45 changes: 45 additions & 0 deletions install/migrations/update_9.5.x_to_10.0.0/stock_thresholds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2021 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI 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 2 of the License, or
* (at your option) any later version.
*
* GLPI 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 GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/

/**
* @var Migration $migration
*/

$migration->addField('glpi_cartridgeitems', 'stock_target', 'int', [
'value' => 0,
'after' => 'alarm_threshold'
]);

$migration->addField('glpi_consumableitems', 'stock_target', 'int', [
'value' => 0,
'after' => 'alarm_threshold'
]);
2 changes: 2 additions & 0 deletions install/mysql/glpi-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ CREATE TABLE `glpi_cartridgeitems` (
`is_deleted` tinyint NOT NULL DEFAULT '0',
`comment` text,
`alarm_threshold` int NOT NULL DEFAULT '10',
`stock_target` int NOT NULL DEFAULT '0',
`date_mod` timestamp NULL DEFAULT NULL,
`date_creation` timestamp NULL DEFAULT NULL,
`pictures` text,
Expand Down Expand Up @@ -1299,6 +1300,7 @@ CREATE TABLE `glpi_consumableitems` (
`is_deleted` tinyint NOT NULL DEFAULT '0',
`comment` text,
`alarm_threshold` int NOT NULL DEFAULT '10',
`stock_target` int NOT NULL DEFAULT '0',
`date_mod` timestamp NULL DEFAULT NULL,
`date_creation` timestamp NULL DEFAULT NULL,
`otherserial` varchar(255) DEFAULT NULL,
Expand Down
8 changes: 4 additions & 4 deletions templates/generic_show_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'name',
item,
(item_type == 'Contact' ? __('Surname') : __('Name')),
withtemplate
withtemplate
) }}
{% endif %}

Expand Down Expand Up @@ -117,9 +117,9 @@
{% if dc_breadcrumbs|length > 0 %}
{% set dc_breadcrumbs %}
<ol class="breadcrumb breadcrumb-arrows" aria-label="breadcrumbs">
{% for dc_item in dc_breadcrumbs|reverse %}
<li class="breadcrumb-item">{{ dc_item|raw }}</li>
{% endfor %}
{% for dc_item in dc_breadcrumbs|reverse %}
<li class="breadcrumb-item">{{ dc_item|raw }}</li>
{% endfor %}
</ol>
{% endset %}

Expand Down
Loading

0 comments on commit 33ff728

Please sign in to comment.