getUnusedNumber() static public method

count how many consumable unused for the consumable item $tID
static public getUnusedNumber ( $tID ) : integer
$tID integer consumable item identifier.
return integer : number of consumable unused counted.
 /**
  * Get all data needed for template processing
  *
  * @param $event
  * @param $options   array
  **/
 function getDatasForTemplate($event, $options = array())
 {
     $events = $this->getAllEvents();
     $this->datas['##consumable.entity##'] = Dropdown::getDropdownName('glpi_entities', $options['entities_id']);
     $this->datas['##lang.consumable.entity##'] = __('Entity');
     $this->datas['##consumable.action##'] = $events[$event];
     foreach ($options['items'] as $id => $consumable) {
         $tmp = array();
         $tmp['##consumable.item##'] = $consumable['name'];
         $tmp['##consumable.reference##'] = $consumable['ref'];
         $tmp['##consumable.remaining##'] = Consumable::getUnusedNumber($id);
         $tmp['##consumable.url##'] = $this->formatURL($options['additionnaloption']['usertype'], "ConsumableItem_" . $id);
         $this->datas['consumables'][] = $tmp;
     }
     $this->getTags();
     foreach ($this->tag_descriptions[NotificationTarget::TAG_LANGUAGE] as $tag => $values) {
         if (!isset($this->datas[$tag])) {
             $this->datas[$tag] = $values['label'];
         }
     }
 }
 /**
  * Get all data needed for template processing
  *
  * @param $event
  * @param $options   array
  **/
 function getDatasForTemplate($event, $options = array())
 {
     global $CFG_GLPI;
     $events = $this->getAllEvents();
     $this->datas['##consumable.entity##'] = Dropdown::getDropdownName('glpi_entities', $options['entities_id']);
     $this->datas['##lang.consumable.entity##'] = __('Entity');
     $this->datas['##consumable.action##'] = $events[$event];
     foreach ($options['items'] as $id => $consumable) {
         $tmp = array();
         $tmp['##consumable.item##'] = $consumable['name'];
         $tmp['##consumable.reference##'] = $consumable['ref'];
         $tmp['##consumable.remaining##'] = Consumable::getUnusedNumber($id);
         $tmp['##consumable.url##'] = urldecode($CFG_GLPI["url_base"] . "/index.php?redirect=consumableitem_" . $id);
         $this->datas['consumables'][] = $tmp;
     }
     $this->getTags();
     foreach ($this->tag_descriptions[NotificationTarget::TAG_LANGUAGE] as $tag => $values) {
         if (!isset($this->datas[$tag])) {
             $this->datas[$tag] = $values['label'];
         }
     }
 }
 /**
  * Cron action on consumables : alert if a stock is behind the threshold
  *
  * @param $task   to log, if NULL display (default NULL)
  *
  * @return 0 : nothing to do 1 : done with success
  **/
 static function cronConsumable($task = NULL)
 {
     global $DB, $CFG_GLPI;
     $cron_status = 1;
     if ($CFG_GLPI["use_mailing"]) {
         $message = array();
         $items = array();
         $alert = new Alert();
         foreach (Entity::getEntitiesToNotify('consumables_alert_repeat') as $entity => $repeat) {
             $query_alert = "SELECT `glpi_consumableitems`.`id` AS consID,\n                                   `glpi_consumableitems`.`entities_id` AS entity,\n                                   `glpi_consumableitems`.`ref` AS ref,\n                                   `glpi_consumableitems`.`name` AS name,\n                                   `glpi_consumableitems`.`alarm_threshold` AS threshold,\n                                   `glpi_alerts`.`id` AS alertID,\n                                   `glpi_alerts`.`date`\n                            FROM `glpi_consumableitems`\n                            LEFT JOIN `glpi_alerts`\n                                 ON (`glpi_consumableitems`.`id` = `glpi_alerts`.`items_id`\n                                     AND `glpi_alerts`.`itemtype`='ConsumableItem')\n                            WHERE `glpi_consumableitems`.`is_deleted` = '0'\n                                  AND `glpi_consumableitems`.`alarm_threshold` >= '0'\n                                  AND `glpi_consumableitems`.`entities_id` = '" . $entity . "'\n                                  AND (`glpi_alerts`.`date` IS NULL\n                                       OR (`glpi_alerts`.date+{$repeat}) < CURRENT_TIMESTAMP());";
             $message = "";
             $items = array();
             foreach ($DB->request($query_alert) as $consumable) {
                 if (($unused = Consumable::getUnusedNumber($consumable["consID"])) <= $consumable["threshold"]) {
                     // define message alert
                     //TRANS: %1$s is the consumable name, %2$s its reference, %3$d the remaining number
                     $message .= sprintf(__('Threshold of alarm reached for the type of consumable: %1$s - Reference %2$s - Remaining %3$d'), $consumable['name'], $consumable['ref'], $unused);
                     $message .= '<br>';
                     $items[$consumable["consID"]] = $consumable;
                     // if alert exists -> delete
                     if (!empty($consumable["alertID"])) {
                         $alert->delete(array("id" => $consumable["alertID"]));
                     }
                 }
             }
             if (!empty($items)) {
                 $options['entities_id'] = $entity;
                 $options['items'] = $items;
                 if (NotificationEvent::raiseEvent('alert', new ConsumableItem(), $options)) {
                     if ($task) {
                         $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " :  {$message}\n");
                         $task->addVolume(1);
                     } else {
                         Session::addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " :  {$message}");
                     }
                     $input["type"] = Alert::THRESHOLD;
                     $input["itemtype"] = 'ConsumableItem';
                     // add alerts
                     foreach ($items as $ID => $consumable) {
                         $input["items_id"] = $ID;
                         $alert->add($input);
                         unset($alert->fields['id']);
                     }
                 } else {
                     $entityname = Dropdown::getDropdownName('glpi_entities', $entity);
                     //TRANS: %s is entity name
                     $msg = sprintf(__('%s: send consumable alert failed'), $entityname);
                     if ($task) {
                         $task->log($msg);
                     } else {
                         Session::addMessageAfterRedirect($msg, false, ERROR);
                     }
                 }
             }
         }
     }
     return $cron_status;
 }
 /**
  * Cron action on consumables : alert if a stock is behind the threshold
  *
  * @param $task to log, if NULL display
  *
  * @return 0 : nothing to do 1 : done with success
  **/
 static function cronConsumable($task = NULL)
 {
     global $DB, $CFG_GLPI, $LANG;
     $cron_status = 1;
     if ($CFG_GLPI["use_mailing"]) {
         $message = array();
         $items = array();
         $alert = new Alert();
         foreach (Entity::getEntitiesToNotify('consumables_alert_repeat') as $entity => $repeat) {
             $query_alert = "SELECT `glpi_consumableitems`.`id` AS consID,\n                                   `glpi_consumableitems`.`entities_id` AS entity,\n                                   `glpi_consumableitems`.`ref` AS consref,\n                                   `glpi_consumableitems`.`name` AS consname,\n                                   `glpi_consumableitems`.`alarm_threshold` AS threshold,\n                                   `glpi_alerts`.`id` AS alertID,\n                                   `glpi_alerts`.`date`\n                            FROM `glpi_consumableitems`\n                            LEFT JOIN `glpi_alerts`\n                                 ON (`glpi_consumableitems`.`id` = `glpi_alerts`.`items_id`\n                                     AND `glpi_alerts`.`itemtype`='ConsumableItem')\n                            WHERE `glpi_consumableitems`.`is_deleted` = '0'\n                                  AND `glpi_consumableitems`.`alarm_threshold` >= '0'\n                                  AND `glpi_consumableitems`.`entities_id` = '" . $entity . "'\n                                  AND (`glpi_alerts`.`date` IS NULL\n                                       OR (`glpi_alerts`.date+{$repeat}) < CURRENT_TIMESTAMP());";
             $message = "";
             $items = array();
             foreach ($DB->request($query_alert) as $consumable) {
                 if (($unused = Consumable::getUnusedNumber($consumable["consID"])) <= $consumable["threshold"]) {
                     // define message alert
                     $message .= $LANG['mailing'][35] . " " . $consumable["consname"] . " - " . $LANG['consumables'][2] . "&nbsp;: " . $consumable["consref"] . " - " . $LANG['software'][20] . "&nbsp;: " . $unused . "<br>";
                     $items[$consumable["consID"]] = $consumable;
                     // if alert exists -> delete
                     if (!empty($consumable["alertID"])) {
                         $alert->delete(array("id" => $consumable["alertID"]));
                     }
                 }
             }
             if (!empty($items)) {
                 $options['entities_id'] = $entity;
                 $options['consumables'] = $items;
                 if (NotificationEvent::raiseEvent('alert', new Consumable(), $options)) {
                     if ($task) {
                         $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " :  {$message}\n");
                         $task->addVolume(1);
                     } else {
                         addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " :  {$message}");
                     }
                     $input["type"] = Alert::THRESHOLD;
                     $input["itemtype"] = 'ConsumableItem';
                     // add alerts
                     foreach ($items as $ID => $consumable) {
                         $input["items_id"] = $ID;
                         $alert->add($input);
                         unset($alert->fields['id']);
                     }
                 } else {
                     if ($task) {
                         $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " : Send consumable alert failed\n");
                     } else {
                         addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " : Send consumable alert failed", false, ERROR);
                     }
                 }
             }
         }
     }
     return $cron_status;
 }