consumableitem.class.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @version $Id: consumableitem.class.php 20337 2013-03-05 10:12:16Z moyo $
00004  -------------------------------------------------------------------------
00005  GLPI - Gestionnaire Libre de Parc Informatique
00006  Copyright (C) 2003-2013 by the INDEPNET Development Team.
00007 
00008  http://indepnet.net/   http://glpi-project.org
00009  -------------------------------------------------------------------------
00010 
00011  LICENSE
00012 
00013  This file is part of GLPI.
00014 
00015  GLPI is free software; you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation; either version 2 of the License, or
00018  (at your option) any later version.
00019 
00020  GLPI is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with GLPI. If not, see <http://www.gnu.org/licenses/>.
00027  --------------------------------------------------------------------------
00028  */
00029 
00030 /** @file
00031 * @brief
00032 */
00033 
00034 if (!defined('GLPI_ROOT')) {
00035    die("Sorry. You can't access directly to this file");
00036 }
00037 
00038 //!  ConsumableItem Class
00039 /**
00040  * This class is used to manage the various types of consumables.
00041  * @see Consumable
00042  * @author Julien Dombre
00043  */
00044 class ConsumableItem extends CommonDBTM {
00045    // From CommonDBTM
00046    static protected $forward_entity_to = array('Consumable', 'Infocom');
00047 
00048    static function getTypeName($nb=0) {
00049 
00050       return _n('Consumable model', 'Consumable models', $nb);
00051    }
00052 
00053 
00054    static function canCreate() {
00055       return Session::haveRight('consumable', 'w');
00056    }
00057 
00058 
00059    static function canView() {
00060       return Session::haveRight('consumable', 'r');
00061    }
00062 
00063 
00064    /**
00065     * @since version 0.84
00066     *
00067     * @see CommonDBTM::getPostAdditionalInfosForName
00068    **/
00069    function getPostAdditionalInfosForName() {
00070 
00071       if (isset($this->fields["ref"]) && !empty($this->fields["ref"])) {
00072          return $this->fields["ref"];
00073       }
00074       return '';
00075    }
00076 
00077 
00078    function cleanDBonPurge() {
00079 
00080       $class = new Consumable();
00081       $class->cleanDBonItemDelete($this->getType(), $this->fields['id']);
00082 
00083       $class = new Alert();
00084       $class->cleanDBonItemDelete($this->getType(), $this->fields['id']);     
00085    }
00086 
00087 
00088    function post_getEmpty() {
00089 
00090      $this->fields["alarm_threshold"] = Entity::getUsedConfig("consumables_alert_repeat",
00091                                                               $this->fields["entities_id"],
00092                                                               "default_consumables_alarm_threshold", 10);
00093    }
00094 
00095 
00096    function defineTabs($options=array()) {
00097 
00098       $ong = array();
00099       $this->addStandardTab('Consumable', $ong, $options);
00100       $this->addStandardTab('Infocom', $ong, $options);
00101       $this->addStandardTab('Document_Item',$ong, $options);
00102       $this->addStandardTab('Link', $ong, $options);
00103       $this->addStandardTab('Note', $ong, $options);
00104 
00105       return $ong;
00106    }
00107 
00108 
00109    /**
00110     * Print the consumable type form
00111     *
00112     * @param $ID        integer ID of the item
00113     * @param $options   array
00114     *     - target filename : where to go when done.
00115     *     - withtemplate boolean : template or basic item
00116     *
00117     * @return Nothing (display)
00118     *
00119     **/
00120    function showForm($ID, $options=array()) {
00121       global $CFG_GLPI;
00122 
00123       $this->initForm($ID, $options);
00124       $this->showTabs($options);
00125       $this->showFormHeader($options);
00126 
00127       echo "<tr class='tab_bg_1'>";
00128       echo "<td>".__('Name')."</td>";
00129       echo "<td>";
00130       Html::autocompletionTextField($this, "name");
00131       echo "</td>";
00132       echo "<td>".__('Type')."</td>";
00133       echo "<td>";
00134       ConsumableItemType::dropdown(array('value' => $this->fields["consumableitemtypes_id"]));
00135       echo "</td></tr>";
00136 
00137       echo "<tr class='tab_bg_1'>";
00138       echo "<td>".__('Reference')."</td>\n";
00139       echo "<td>";
00140       Html::autocompletionTextField($this, "ref");
00141       echo "</td>";
00142       echo "<td>".__('Manufacturer')."</td>";
00143       echo "<td>";
00144       Manufacturer::dropdown(array('value' => $this->fields["manufacturers_id"]));
00145       echo "</td></tr>\n";
00146 
00147       echo "<tr class='tab_bg_1'>";
00148       echo "<td>".__('Technician in charge of the hardware')."</td>";
00149       echo "<td>";
00150       User::dropdown(array('name'   => 'users_id_tech',
00151                            'value'  => $this->fields["users_id_tech"],
00152                            'right'  => 'own_ticket',
00153                            'entity' => $this->fields["entities_id"]));
00154       echo "</td>";
00155       echo "<td rowspan='4' class='middle'>".__('Comments')."</td>";
00156       echo "<td class='middle' rowspan='4'>
00157              <textarea cols='45' rows='9' name='comment' >".$this->fields["comment"]."</textarea>";
00158       echo "</td></tr>";
00159 
00160       echo "<tr class='tab_bg_1'>";
00161       echo "<td>".__('Group in charge of the hardware')."</td>";
00162       echo "<td>";
00163       Group::dropdown(array('name'      => 'groups_id_tech',
00164                             'value'     => $this->fields['groups_id_tech'],
00165                             'entity'    => $this->fields['entities_id'],
00166                             'condition' => '`is_assign`'));
00167       echo "</td></tr>\n";
00168 
00169       echo "<tr class='tab_bg_1'>";
00170       echo "<td>".__('Stock location')."</td>";
00171       echo "<td>";
00172       Location::dropdown(array('value'  => $this->fields["locations_id"],
00173                                'entity' => $this->fields["entities_id"]));
00174       echo "</td></tr>";
00175 
00176       echo "<tr class='tab_bg_1'>";
00177       echo "<td>".__('Alert threshold')."</td>";
00178       echo "<td>";
00179       Dropdown::showInteger('alarm_threshold', $this->fields["alarm_threshold"], 0, 100, 1,
00180                             array('-1' => __('Never')));
00181 
00182       Alert::displayLastAlert('ConsumableItem', $ID);
00183       echo "</td></tr>";
00184 
00185       $this->showFormButtons($options);
00186       $this->addDivForTabs();
00187 
00188       return true;
00189    }
00190 
00191 
00192    /**
00193     * @see CommonDBTM::getSpecificMassiveActions()
00194    **/
00195    function getSpecificMassiveActions($checkitem=NULL) {
00196 
00197       $isadmin = static::canUpdate();
00198       $actions = parent::getSpecificMassiveActions($checkitem);
00199 
00200       if (Session::haveRight('transfer','r')
00201           && Session::isMultiEntitiesMode()
00202           && $isadmin) {
00203          $actions['add_transfer_list'] = _x('button', 'Add to transfer list');
00204       }
00205       return $actions;
00206    }
00207 
00208 
00209    function getSearchOptions() {
00210 
00211       $tab = array();
00212       $tab['common']             = __('Characteristics');
00213 
00214       $tab[1]['table']           = $this->getTable();
00215       $tab[1]['field']           = 'name';
00216       $tab[1]['name']            = __('Name');
00217       $tab[1]['datatype']        = 'itemlink';
00218       $tab[1]['massiveaction']   = false;
00219 
00220       $tab[2]['table']           = $this->getTable();
00221       $tab[2]['field']           = 'id';
00222       $tab[2]['name']            = __('ID');
00223       $tab[2]['datatype']        = 'number';
00224       $tab[2]['massiveaction']   = false;
00225 
00226       $tab[34]['table']          = $this->getTable();
00227       $tab[34]['field']          = 'ref';
00228       $tab[34]['name']           = __('Reference');
00229       $tab[34]['datatype']       = 'string';
00230 
00231       $tab[4]['table']           = 'glpi_consumableitemtypes';
00232       $tab[4]['field']           = 'name';
00233       $tab[4]['name']            = __('Type');
00234       $tab[4]['datatype']        = 'dropdown';
00235 
00236       $tab[23]['table']          = 'glpi_manufacturers';
00237       $tab[23]['field']          = 'name';
00238       $tab[23]['name']           = __('Manufacturer');
00239       $tab[23]['datatype']       = 'dropdown';
00240 
00241       $tab += Location::getSearchOptionsToAdd();
00242 
00243       $tab[24]['table']          = 'glpi_users';
00244       $tab[24]['field']          = 'name';
00245       $tab[24]['linkfield']      = 'users_id_tech';
00246       $tab[24]['name']           = __('Technician in charge of the hardware');
00247       $tab[24]['datatype']       = 'dropdown';
00248       $tab[24]['right']          = 'own_ticket';
00249 
00250       $tab[49]['table']          = 'glpi_groups';
00251       $tab[49]['field']          = 'completename';
00252       $tab[49]['linkfield']      = 'groups_id_tech';
00253       $tab[49]['name']           = __('Group in charge of the hardware');
00254       $tab[49]['condition']      = '`is_assign`';
00255       $tab[49]['datatype']       = 'dropdown';
00256 
00257       $tab[8]['table']           = $this->getTable();
00258       $tab[8]['field']           = 'alarm_threshold';
00259       $tab[8]['name']            = __('Alert threshold');
00260       $tab[8]['datatype']        = 'number';
00261       $tab[8]['toadd']           = array('-1' => __('Never'));
00262 
00263       $tab[16]['table']          = $this->getTable();
00264       $tab[16]['field']          = 'comment';
00265       $tab[16]['name']           = __('Comments');
00266       $tab[16]['datatype']       = 'text';
00267 
00268       $tab[90]['table']          = $this->getTable();
00269       $tab[90]['field']          = 'notepad';
00270       $tab[90]['name']           = __('Notes');
00271       $tab[90]['massiveaction']  = false;
00272       $tab[90]['datatype']       = 'text';
00273 
00274 
00275       $tab[80]['table']          = 'glpi_entities';
00276       $tab[80]['field']          = 'completename';
00277       $tab[80]['name']           = __('Entity');
00278       $tab[80]['massiveaction']  = false;
00279       $tab[80]['datatype']       = 'dropdown';
00280 
00281       return $tab;
00282    }
00283 
00284 
00285    static function cronInfo($name) {
00286       return array('description' => __('Send alarms on consumables'));
00287    }
00288 
00289 
00290    /**
00291     * Cron action on consumables : alert if a stock is behind the threshold
00292     *
00293     * @param $task   to log, if NULL display (default NULL)
00294     *
00295     * @return 0 : nothing to do 1 : done with success
00296    **/
00297    static function cronConsumable($task=NULL) {
00298       global $DB, $CFG_GLPI;
00299 
00300       $cron_status = 1;
00301 
00302       if ($CFG_GLPI["use_mailing"]) {
00303          $message = array();
00304          $items   = array();
00305          $alert   = new Alert();
00306 
00307          foreach (Entity::getEntitiesToNotify('consumables_alert_repeat') as $entity => $repeat) {
00308 
00309             $query_alert = "SELECT `glpi_consumableitems`.`id` AS consID,
00310                                    `glpi_consumableitems`.`entities_id` AS entity,
00311                                    `glpi_consumableitems`.`ref` AS ref,
00312                                    `glpi_consumableitems`.`name` AS name,
00313                                    `glpi_consumableitems`.`alarm_threshold` AS threshold,
00314                                    `glpi_alerts`.`id` AS alertID,
00315                                    `glpi_alerts`.`date`
00316                             FROM `glpi_consumableitems`
00317                             LEFT JOIN `glpi_alerts`
00318                                  ON (`glpi_consumableitems`.`id` = `glpi_alerts`.`items_id`
00319                                      AND `glpi_alerts`.`itemtype`='ConsumableItem')
00320                             WHERE `glpi_consumableitems`.`is_deleted` = '0'
00321                                   AND `glpi_consumableitems`.`alarm_threshold` >= '0'
00322                                   AND `glpi_consumableitems`.`entities_id` = '".$entity."'
00323                                   AND (`glpi_alerts`.`date` IS NULL
00324                                        OR (`glpi_alerts`.date+$repeat) < CURRENT_TIMESTAMP());";
00325             $message = "";
00326             $items   = array();
00327 
00328             foreach ($DB->request($query_alert) as $consumable) {
00329                if (($unused=Consumable::getUnusedNumber($consumable["consID"]))
00330                               <=$consumable["threshold"]) {
00331                   // define message alert
00332                   //TRANS: %1$s is the consumable name, %2$s its reference, %3$d the remaining number
00333                   $message .= sprintf(__('Threshold of alarm reached for the type of consumable: %1$s - Reference %2$s - Remaining %3$d'),
00334                                       $consumable['name'], $consumable['ref'], $unused);
00335                   $message.='<br>';
00336 
00337                   $items[$consumable["consID"]] = $consumable;
00338 
00339                   // if alert exists -> delete
00340                   if (!empty($consumable["alertID"])) {
00341                      $alert->delete(array("id" => $consumable["alertID"]));
00342                   }
00343                }
00344             }
00345 
00346             if (!empty($items)) {
00347                $options['entities_id'] = $entity;
00348                $options['items']       = $items;
00349 
00350                if (NotificationEvent::raiseEvent('alert', new ConsumableItem(), $options)) {
00351                   if ($task) {
00352                      $task->log(Dropdown::getDropdownName("glpi_entities",
00353                                                           $entity)." :  $message\n");
00354                      $task->addVolume(1);
00355                   } else {
00356                      Session::addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities",
00357                                                                                 $entity).
00358                                                       " :  $message");
00359                   }
00360 
00361                   $input["type"]     = Alert::THRESHOLD;
00362                   $input["itemtype"] = 'ConsumableItem';
00363 
00364                   // add alerts
00365                   foreach ($items as $ID=>$consumable) {
00366                      $input["items_id"] = $ID;
00367                      $alert->add($input);
00368                      unset($alert->fields['id']);
00369                   }
00370 
00371                } else {
00372                   $entityname = Dropdown::getDropdownName('glpi_entities', $entity);
00373                   //TRANS: %s is entity name
00374                   $msg = sprintf(__('%s: send consumable alert failed'), $entityname);
00375                   if ($task) {
00376                      $task->log($msg);
00377                   } else {
00378                      Session::addMessageAfterRedirect($msg, false, ERROR);
00379                   }
00380                }
00381             }
00382           }
00383       }
00384       return $cron_status;
00385    }
00386 
00387 
00388    function getEvents() {
00389       return array('alert' => __('Send alarms on consumables'));
00390    }
00391 
00392 
00393    /**
00394     * Display debug information for current object
00395    **/
00396    function showDebug() {
00397 
00398       // see query_alert in cronConsumable()
00399       $item = array('consID'    => $this->fields['id'],
00400                     'entity'    => $this->fields['entities_id'],
00401                     'ref'       => $this->fields['ref'],
00402                     'name'      => $this->fields['name'],
00403                     'threshold' => $this->fields['alarm_threshold']);
00404 
00405       $options = array();
00406       $options['entities_id'] = $this->getEntityID();
00407       $options['items']       = array($item);
00408       NotificationEvent::debugEvent($this, $options);
00409    }
00410 
00411 }
00412 ?>