Skip to content

Commit

Permalink
Add credit consumed notification (#131)
Browse files Browse the repository at this point in the history
* Add remaining quantity alert feature

* fix evenement name to raise

* update labels

* pass CI checks

* fix identation

* add low_credit_alert field in case table already exists

* Update inc/entity.class.php

Co-authored-by: Romain B. <[email protected]>

* add PHPDoc tag @var for global variables

* define translation var where it is needed

* update log

---------

Co-authored-by: Romain B. <[email protected]>
  • Loading branch information
bessantoy and Rom1-B authored Jun 25, 2024
1 parent 1182343 commit bbc4a93
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 2 deletions.
10 changes: 10 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ function plugin_credit_install()
]
);

CronTask::register(
'PluginCreditEntity',
'lowcredits',
DAY_TIMESTAMP,
[
'comment' => '',
'mode' => CronTask::MODE_INTERNAL,
]
);

return true;
}

Expand Down
101 changes: 101 additions & 0 deletions inc/entity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
$out .= "</td>";
$out .= "</tr>";
$out .= "<tr class='tab_bg_1'>";
$out .= "<td colspan='3'>";
$out .= __('Low credit alert threshold') . "</td><td>";
$out .= Dropdown::showNumber("low_credit_alert", ['value' => -1,
'min' => 0,
'max' => 50,
'step' => 10,
'toadd' => [-1 => __('Disabled')],
'display' => false,
'unit' => '%'
]);
$out .= "</td>";
$out .= "</tr>";
$out .= "<tr class='tab_bg_1'>";
$out .= "<td class='tab_bg_2 center' colspan='8'>";
$out .= "<input type='submit' name='add' value='" . _sx('button', 'Add') . "' class='submit'>";
$out .= "</td>";
Expand Down Expand Up @@ -267,6 +280,7 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
$header_end .= "<th>" . __('Quantity consumed', 'credit') . "</th>";
$header_end .= "<th>" . __('Quantity remaining', 'credit') . "</th>";
$header_end .= "<th>" . __('Allow overconsumption', 'credit') . "</th>";
$header_end .= "<th>" . __('Low credits alert') . "</th>";
$header_end .= "<th>" . __('Entity') . "</th>";
$header_end .= "<th>" . __('Child entities') . "</th>";
$header_end .= "</tr>";
Expand Down Expand Up @@ -340,6 +354,10 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
$out .= ($data["overconsumption_allowed"]) ? __('Yes') : __('No');
$out .= "</td>";

$out .= "<td>";
$out .= $data["low_credit_alert"] == -1 ? __('Disabled') : $data["low_credit_alert"] . '%';
$out .= "</td>";

$out .= "<td>";
$out .= Dropdown::getDropdownName(Entity::getTable(), $data['entities_id']);
$out .= "</td>";
Expand Down Expand Up @@ -420,6 +438,19 @@ public function rawSearchOptions()
'datatype' => 'bool',
];

$tab[] = [
'id' => 997,
'table' => self::getTable(),
'field' => 'low_credit_alert',
'name' => __('Low credit alert', 'credit'),
'datatype' => 'number',
'min' => 0,
'max' => 50,
'step' => 10,
'toadd' => [-1 => __('Disabled')],
'unit' => '%'
];

return $tab;
}

Expand All @@ -431,6 +462,10 @@ public static function cronInfo($name)
'description' => __('Expiration date', 'credit'),
'parameter' => __('Notice (in days)', 'credit')
];
case 'lowcredits':
return [
'description' => __('Low credits', 'credit'),
];
}
return [];
}
Expand Down Expand Up @@ -512,6 +547,68 @@ public static function cronCreditExpired($task)
return 1;
}

public static function cronLowCredits($task)
{
/**
* @var array $CFG_GLPI
* @var DBmysql $DB
*/
global $CFG_GLPI, $DB;

if (!$CFG_GLPI['use_notifications']) {
return 0;
}

$alert = new Alert();
$credits_iterator = $DB->request(
[
'SELECT' => [
'glpi_plugin_credit_entities.id',
'glpi_plugin_credit_entities.name',
],
'FROM' => 'glpi_plugin_credit_entities',
'LEFT JOIN' => [
'glpi_plugin_credit_tickets' => [
'ON' => [
'glpi_plugin_credit_tickets' => 'plugin_credit_entities_id',
'glpi_plugin_credit_entities' => 'id',
]
]
],
'WHERE' => [
'glpi_plugin_credit_entities.is_active' => 1,
],
'GROUPBY' => 'glpi_plugin_credit_entities.id',
'HAVING' => new QueryExpression('glpi_plugin_credit_entities.quantity - quantity_consumed <= (glpi_plugin_credit_entities.quantity * glpi_plugin_credit_entities.low_credit_alert) / 100')
]
);

foreach ($credits_iterator as $credit_data) {
$task->addVolume(1);
$task->log(
sprintf(
'Low credit for %s',
$credit_data['name'],
)
);

$credit = new PluginCreditEntity();
$credit->getFromDB($credit_data['id']);

NotificationEvent::raiseEvent('lowcredits', $credit);

$input = [
'type' => Alert::END,
'itemtype' => self::getType(),
'items_id' => $credit_data['id'],
];
$alert->add($input);
unset($alert->fields['id']);
}

return 1;
}

/**
* Install all necessary tables for the plugin
*
Expand Down Expand Up @@ -541,6 +638,7 @@ public static function install(Migration $migration)
`end_date` timestamp NULL DEFAULT NULL,
`quantity` int NOT NULL DEFAULT '0',
`overconsumption_allowed` tinyint NOT NULL DEFAULT '0',
`low_credit_alert` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `entities_id` (`entities_id`),
Expand All @@ -562,6 +660,9 @@ public static function install(Migration $migration)

// 1.10.0
$migration->dropField($table, 'is_default'); // Was created during dev phase of 1.10.0, then removed

// 1.13.2
$migration->addField($table, 'low_credit_alert', 'int', ['update' => "NULL"]);
}

return true;
Expand Down
131 changes: 129 additions & 2 deletions inc/notificationtargetentity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class PluginCreditNotificationTargetEntity extends NotificationTarget
public function getEvents()
{
return [
'expired' => __('Expiration date', 'credit')
'expired' => __('Expiration date', 'credit'),
'lowcredits' => __('Low credits', 'credit')
];
}

Expand Down Expand Up @@ -116,7 +117,9 @@ public function getTags()

$lang = [
'credit.expired' => __('Credit voucher expiration', 'credit'),
'credit.expired.information' => __('This credit voucher will expire soon. Please, consider buying a new one.', 'credit')
'credit.expired.information' => __('This credit voucher will expire soon. Please, consider buying a new one.', 'credit'),
'credit.lowcredits' => __('Amount of credit remaining close to or at 0', 'credit'),
'credit.lowcredits.information' => __('The remaining quantity has reached 0 or almost.', 'credit'),
];

foreach ($lang as $tag => $label) {
Expand Down Expand Up @@ -227,6 +230,87 @@ public static function install(Migration $migration)
);
}
}

$templates_id_quantity = false;
$result_quantity = $DB->request(
[
'SELECT' => 'id',
'FROM' => 'glpi_notificationtemplates',
'WHERE' => [
'itemtype' => 'PluginCreditEntity',
'name' => 'Low credits',
]
]
);

if (count($result_quantity) > 0) {
$data_quantity = $result_quantity->current();
$templates_id_quantity = $data_quantity['id'];
} else {
$templates_id_quantity = $template->add(
[
'name' => 'Low credits',
'itemtype' => 'PluginCreditEntity',
'date_mod' => $_SESSION['glpi_currenttime'],
'comment' => '',
'css' => '',
]
);
}

if ($templates_id_quantity) {
$translation_count_quantity = countElementsInTable(
$translation->getTable(),
['notificationtemplates_id' => $templates_id_quantity]
);
if ($translation_count_quantity == 0) {
$translation->add(
[
'notificationtemplates_id' => $templates_id_quantity,
'language' => '',
'subject' => '##lang.credit.lowcredits## : ##credit.name##',
'content_text' => '##lang.credit.lowcredits.information##',
'content_html' => '##lang.credit.lowcredits.information##',
]
);
}

$notifications_count_quantity = countElementsInTable(
$notification->getTable(),
['itemtype' => 'PluginCreditEntity', 'event' => 'lowcredits']
);

if ($notifications_count_quantity == 0) {
$notification_id_quantity = $notification->add(
[
'name' => 'Low credits',
'entities_id' => 0,
'itemtype' => 'PluginCreditEntity',
'event' => 'lowcredits',
'comment' => '',
'is_recursive' => 1,
'is_active' => 1,
'date_mod' => $_SESSION['glpi_currenttime'],
]
);

$n_n_template->add(
[
'notifications_id' => $notification_id_quantity,
'mode' => Notification_NotificationTemplate::MODE_MAIL,
'notificationtemplates_id' => $templates_id_quantity,
]
);

$target->add(
[
'notifications_id' => $notification_id_quantity,
'type' => Notification::USER_TYPE,
'items_id' => Notification::ENTITY_ADMINISTRATOR,
]
);
}
}
}

public static function uninstall()
Expand Down Expand Up @@ -276,5 +360,48 @@ public static function uninstall()

$template->delete($template_data);
}

$notifications_iterator_quantity = $DB->request(
[
'SELECT' => 'id',
'FROM' => $notification->getTable(),
'WHERE' => [
'itemtype' => 'PluginCreditEntity',
'event' => 'lowcredits',
],
]
);
foreach ($notifications_iterator_quantity as $notification_data_quantity) {
$notification->delete($notification_data_quantity);
}

$templates_iterator_quantity = $DB->request(
[
'SELECT' => 'id',
'FROM' => $template->getTable(),
'WHERE' => [
'itemtype' => 'PluginCreditEntity',
'name' => 'lowcredits',
],
]
);
foreach ($templates_iterator_quantity as $template_data_quantity) {
$translation = new NotificationTemplateTranslation();
$translations_iterator_quantity = $DB->request(
[
'SELECT' => 'id',
'FROM' => $translation->getTable(),
'WHERE' => [
'notificationtemplates_id' => $template_data_quantity['id'],
],
]
);
foreach ($translations_iterator_quantity as $translation_data_quantity) {
$translation = new NotificationTemplateTranslation();
$translation->delete($translation_data_quantity);
}

$template->delete($template_data_quantity);
}
}
}

0 comments on commit bbc4a93

Please sign in to comment.