grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026. -------------------------------------------------------------------------- */ /** @file * @brief */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Budget class */ class Budget extends CommonDropdown{ // From CommonDBTM public $dohistory = true; static $rightname = 'budget'; protected $usenotepad = true; public $can_be_translated = false; static function getTypeName($nb=0) { return _n('Budget', 'Budgets', $nb); } function defineTabs($options=array()) { $ong = array(); $this->addDefaultFormTab($ong); $this->addStandardTab(__CLASS__,$ong, $options); $this->addStandardTab('Document_Item',$ong, $options); $this->addStandardTab('Link',$ong, $options); $this->addStandardTab('Notepad',$ong, $options); $this->addStandardTab('Log',$ong, $options); return $ong; } function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { if (!$withtemplate) { switch ($item->getType()) { case __CLASS__ : return array(1 => __('Main'), 2 => _n('Item', 'Items', Session::getPluralNumber())); } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { if ($item->getType() == __CLASS__) { switch ($tabnum) { case 1 : $item->showValuesByEntity(); break; case 2 : $item->showItems(); break; } } return true; } /** * Print the contact form * * @param $ID integer ID of the item * @param $options array of possible options: * - target for the Form * - withtemplate : template or basic item * * @return Nothing (display) **/ function showForm($ID, $options=array()) { $rowspan = 3; if ($ID > 0) { $rowspan++; } $this->initForm($ID, $options); $this->showFormHeader($options); echo ""; echo "".__('Name').""; echo ""; Html::autocompletionTextField($this, "name"); echo ""; echo "".__('Type').""; echo ""; Dropdown::show('BudgetType', array('value' => $this->fields['budgettypes_id'])); echo ""; echo ""; echo ""._x('price', 'Value').""; echo ""; echo "".__('Comments').""; echo "". "". ""; echo ""; echo "".__('Start date').""; echo ""; Html::showDateField("begin_date", array('value' => $this->fields["begin_date"])); echo ""; echo ""; echo "".__('End date').""; echo ""; Html::showDateField("end_date", array('value' => $this->fields["end_date"])); echo ""; echo ""; echo "".__('Location').""; echo ""; Location::dropdown(array('value' => $this->fields["locations_id"], 'entity' => $this->fields["entities_id"])); echo ""; $this->showFormButtons($options); return true; } function prepareInputForAdd($input) { if (isset($input["id"]) && ($input["id"] > 0)) { $input["_oldID"] = $input["id"]; } unset($input['id']); unset($input['withtemplate']); return $input; } function post_addItem() { global $DB; // Manage add from template if (isset($this->input["_oldID"])) { // ADD Documents Document_Item::cloneItem($this->getType(), $this->input["_oldID"], $this->fields['id']); } } function getSearchOptions() { $tab = array(); $tab[1]['table'] = $this->getTable(); $tab[1]['field'] = 'name'; $tab[1]['name'] = __('Name'); $tab[1]['datatype'] = 'itemlink'; $tab[1]['massiveaction'] = false; $tab[2]['table'] = $this->getTable(); $tab[2]['field'] = 'id'; $tab[2]['name'] = __('ID'); $tab[2]['massiveaction'] = false; $tab[2]['datatype'] = 'number'; $tab[19]['table'] = $this->getTable(); $tab[19]['field'] = 'date_mod'; $tab[19]['name'] = __('Last update'); $tab[19]['datatype'] = 'datetime'; $tab[19]['massiveaction'] = false; $tab[121]['table'] = $this->getTable(); $tab[121]['field'] = 'date_creation'; $tab[121]['name'] = __('Creation date'); $tab[121]['datatype'] = 'datetime'; $tab[121]['massiveaction'] = false; $tab[4]['table'] = 'glpi_budgettypes'; $tab[4]['field'] = 'name'; $tab[4]['name'] = __('Type'); $tab[4]['datatype'] = 'dropdown'; $tab[5]['table'] = $this->getTable(); $tab[5]['field'] = 'begin_date'; $tab[5]['name'] = __('Start date'); $tab[5]['datatype'] = 'date'; $tab[6]['table'] = $this->getTable(); $tab[6]['field'] = 'end_date'; $tab[6]['name'] = __('End date'); $tab[6]['datatype'] = 'date'; $tab[7]['table'] = $this->getTable(); $tab[7]['field'] = 'value'; $tab[7]['name'] = _x('price', 'Value'); $tab[7]['datatype'] = 'decimal'; $tab[16]['table'] = $this->getTable(); $tab[16]['field'] = 'comment'; $tab[16]['name'] = __('Comments'); $tab[16]['datatype'] = 'text'; $tab[80]['table'] = 'glpi_entities'; $tab[80]['field'] = 'completename'; $tab[80]['name'] = __('Entity'); $tab[80]['massiveaction'] = false; $tab[80]['datatype'] = 'dropdown'; $tab[86]['table'] = $this->getTable(); $tab[86]['field'] = 'is_recursive'; $tab[86]['name'] = __('Child entities'); $tab[86]['datatype'] = 'bool'; // add objectlock search options $tab += ObjectLock::getSearchOptionsToAdd( get_class($this) ) ; $tab += Location::getSearchOptionsToAdd(); $tab += Notepad::getSearchOptionsToAdd(); return $tab; } /** * Print the HTML array of Items on a budget * * @return Nothing (display) **/ function showItems() { global $DB; $budgets_id = $this->fields['id']; if (!$this->can($budgets_id, READ)) { return false; } $query = "SELECT DISTINCT `itemtype` FROM `glpi_infocoms` WHERE `budgets_id` = '$budgets_id' AND itemtype NOT IN ('ConsumableItem', 'CartridgeItem', 'Software') ORDER BY `itemtype`"; $result = $DB->query($query); $number = $DB->numrows($result); echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; $num = 0; $itemtypes = array(); for ($i = 0; $i < $number ; $i++) { $itemtypes[] = $DB->result($result, $i, "itemtype"); } $itemtypes[] = 'Contract'; $itemtypes[] = 'Ticket'; $itemtypes[] = 'Problem'; $itemtypes[] = 'Change'; $itemtypes[] = 'Project'; foreach ($itemtypes as $itemtype) { if (!($item = getItemForItemtype($itemtype))) { continue; } if ($item->canView()) { switch ($itemtype) { case 'Contract' : $query = "SELECT `".$item->getTable()."`.`id`, `".$item->getTable()."`.`entities_id`, SUM(`glpi_contractcosts`.`cost`) as value FROM `glpi_contractcosts` INNER JOIN `".$item->getTable()."` ON (`".$item->getTable()."`.`id` = `glpi_contractcosts`.`contracts_id`) WHERE `glpi_contractcosts`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $item->getTable())." AND NOT `".$item->getTable()."`.`is_template` GROUP BY `".$item->getTable()."`.`id`, `".$item->getTable()."`.`entities_id` ORDER BY `".$item->getTable()."`.`entities_id`, `".$item->getTable()."`.`name`"; break; case 'Ticket' : case 'Problem' : case 'Change' : $costtable = getTableForItemType($item->getType().'Cost'); $query = "SELECT `".$item->getTable()."`.`id`, `".$item->getTable()."`.`entities_id`, SUM(`$costtable`.`actiontime`*`$costtable`.`cost_time`/".HOUR_TIMESTAMP." + `$costtable`.`cost_fixed` + `$costtable`.`cost_material`) as value FROM `$costtable` INNER JOIN `".$item->getTable()."` ON (`".$item->getTable()."`.`id` = `$costtable`.`".$item->getForeignKeyField()."`) WHERE `$costtable`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $item->getTable())." GROUP BY `".$item->getTable()."`.`id`, `".$item->getTable()."`.`entities_id` ORDER BY `".$item->getTable()."`.`entities_id`, `".$item->getTable()."`.`name`"; break; case 'Project' : $query = "SELECT `".$item->getTable()."`.`id`, `".$item->getTable()."`.`entities_id`, SUM(`glpi_projectcosts`.`cost`) as value FROM `glpi_projectcosts` INNER JOIN `".$item->getTable()."` ON (`".$item->getTable()."`.`id` = `glpi_projectcosts`.`projects_id`) WHERE `glpi_projectcosts`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $item->getTable())." GROUP BY `".$item->getTable()."`.`id`, `".$item->getTable()."`.`entities_id` ORDER BY `".$item->getTable()."`.`entities_id`, `".$item->getTable()."`.`name`"; break; case 'Cartridge' : $query = "SELECT `".$item->getTable()."`.*, `glpi_cartridgeitems`.`name`, `glpi_infocoms`.`value` FROM `glpi_infocoms` INNER JOIN `".$item->getTable()."` ON (`".$item->getTable()."`.`id` = `glpi_infocoms`.`items_id`) INNER JOIN `glpi_cartridgeitems` ON (`".$item->getTable()."`.`cartridgeitems_id` = `glpi_cartridgeitems`.`id`) WHERE `glpi_infocoms`.`itemtype`='$itemtype' AND `glpi_infocoms`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $item->getTable())." ORDER BY `entities_id`, `glpi_cartridgeitems`.`name`"; break; case 'Consumable' : $query = "SELECT `".$item->getTable()."`.*, `glpi_consumableitems`.`name`, `glpi_infocoms`.`value` FROM `glpi_infocoms` INNER JOIN `".$item->getTable()."` ON (`".$item->getTable()."`.`id` = `glpi_infocoms`.`items_id`) INNER JOIN `glpi_consumableitems` ON (`".$item->getTable()."`.`consumableitems_id` = `glpi_consumableitems`.`id`) WHERE `glpi_infocoms`.`itemtype` = '$itemtype' AND `glpi_infocoms`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $item->getTable())." ORDER BY `entities_id`, `glpi_consumableitems`.`name`"; break; default : $query = "SELECT `".$item->getTable()."`.*, `glpi_infocoms`.`value` FROM `glpi_infocoms` INNER JOIN `".$item->getTable()."` ON (`".$item->getTable()."`.`id` = `glpi_infocoms`.`items_id`) WHERE `glpi_infocoms`.`itemtype` = '$itemtype' AND `glpi_infocoms`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $item->getTable())." ".($item->maybeTemplate()?" AND NOT `".$item->getTable()."`.`is_template`":'')." ORDER BY `".$item->getTable()."`.`entities_id`,"; if ($item instanceof Item_Devices) { $query .= " `".$item->getTable()."`.`itemtype`"; } else { $query .= " `".$item->getTable()."`.`name`"; } break; } if ($result_linked = $DB->query($query)) { $nb = $DB->numrows($result_linked); if ($nb > $_SESSION['glpilist_limit']) { echo ""; $name = $item->getTypeName($nb); //TRANS: %1$s is a name, %2$s is a number echo ""; echo ""; echo ""; } else if ($nb) { for ($prem=true ; $data=$DB->fetch_assoc($result_linked) ; $prem=false) { $name = NOT_AVAILABLE; if ($item->getFromDB($data["id"])) { if ($item instanceof Item_Devices) { $tmpitem = new $item::$itemtype_2(); if ($tmpitem->getFromDB($data[$item::$items_id_2])) { $name = $tmpitem->getLink(array('additional' => true)); } } else { $name = $item->getLink(array('additional' => true)); } } echo ""; if ($prem) { $typename = $item->getTypeName($nb); echo ""; } echo ""; echo ""; echo ""; echo ""; } } $num += $nb; } } } if ($num>0) { echo ""; echo ""; echo " "; } echo "
"; Html::printPagerForm(); echo ""; if ($DB->numrows($result) == 0) { _e('No associated item'); } else { echo _n('Associated item', 'Associated items', $DB->numrows($result)); } echo "
".__('Type')."".__('Entity')."".__('Name')."".__('Serial number')."".__('Inventory number').""._x('price', 'Value')."
".sprintf(__('%1$s: %2$s'), $name, $nb).""; $opt = array('order' => 'ASC', 'is_deleted' => 0, 'reset' => 'reset', 'start' => 0, 'sort' => 80, 'criteria' => array(0 => array('value' => '$$$$'.$budgets_id, 'searchtype' => 'contains', 'field' => 50))); echo "". __('Device list')."---". "
". ($nb>1 ? sprintf(__('%1$s: %2$s'), $typename, $nb) : $typename)."".Dropdown::getDropdownName("glpi_entities", $data["entities_id"]); echo "".$name."".(isset($data["serial"])? "".$data["serial"]."" :"-"); echo "". (isset($data["otherserial"])? "".$data["otherserial"]."" :"-")."". (isset($data["value"]) ? "".Html::formatNumber($data["value"], true)."" :"-"); echo "
".sprintf(__('%1$s = %2$s'), __('Total'), $num)." 
"; } /** * Print the HTML array of value consumed for a budget * * @return Nothing (display) **/ function showValuesByEntity() { global $DB; $budgets_id = $this->fields['id']; if (!$this->can($budgets_id, READ)) { return false; } // Type for which infocom are only template $ignore = array('CartridgeItem', 'ConsumableItem', 'Software'); $query = "SELECT DISTINCT `itemtype` FROM `glpi_infocoms` WHERE `budgets_id` = '$budgets_id' AND `itemtype` NOT IN ('".implode("','",$ignore)."')". getEntitiesRestrictRequest(" AND", 'glpi_infocoms', "entities_id")." GROUP BY `itemtype`"; $result = $DB->query($query); $total = 0; $totalbytypes = array(); $itemtypes = array(); $entities_values = array(); $entitiestype_values = array(); $found_types = array(); if ($DB->numrows($result)) { while ($types = $DB->fetch_assoc($result)) { $itemtypes[] = $types['itemtype']; } } $itemtypes[] = 'Contract'; $itemtypes[] = 'Ticket'; $itemtypes[] = 'Problem'; $itemtypes[] = 'Project'; $itemtypes[] = 'Change'; foreach ($itemtypes as $itemtype) { if (!($item = getItemForItemtype($itemtype))) { continue; } $table = getTableForItemType($itemtype); switch ($itemtype) { case 'Contract' : $query_infos = "SELECT SUM(`glpi_contractcosts`.`cost`) AS `sumvalue`, `$table`.`entities_id` FROM `glpi_contractcosts` INNER JOIN `$table` ON (`glpi_contractcosts`.`contracts_id` = `$table`.`id`) WHERE `glpi_contractcosts`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $table, "entities_id")." AND `$table`.`is_template` = '0' GROUP BY `$table`.`entities_id`"; break; case 'Project' : $costtable = getTableForItemType($item->getType().'Cost'); $query_infos = "SELECT SUM(`glpi_projectcosts`.`cost`) AS `sumvalue`, `$table`.`entities_id` FROM `glpi_projectcosts` INNER JOIN `$table` ON (`glpi_projectcosts`.`projects_id` = `$table`.`id`) WHERE `glpi_projectcosts`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $table, "entities_id")." GROUP BY `$table`.`entities_id`"; break; case 'Ticket' : case 'Problem' : case 'Change' : $costtable = getTableForItemType($item->getType().'Cost'); $query_infos = "SELECT SUM(`$costtable`.`actiontime`*`$costtable`.`cost_time`/".HOUR_TIMESTAMP." + `$costtable`.`cost_fixed` + `$costtable`.`cost_material`) AS `sumvalue`, `$table`.`entities_id` FROM `$costtable` INNER JOIN `$table` ON (`$costtable`.`".$item->getForeignKeyField()."` = `$table`.`id`) WHERE `$costtable`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $table, "entities_id")." GROUP BY `$table`.`entities_id`"; break; default : $query_infos = "SELECT SUM(`glpi_infocoms`.`value`) AS `sumvalue`, `$table`.`entities_id` FROM `$table` INNER JOIN `glpi_infocoms` ON (`glpi_infocoms`.`items_id` = `$table`.`id` AND `glpi_infocoms`.`itemtype` = '$itemtype') WHERE `glpi_infocoms`.`budgets_id` = '$budgets_id' ". getEntitiesRestrictRequest(" AND", $table, "entities_id"); if ($item->maybeTemplate()) { $query_infos .= " AND `$table`.`is_template` = '0' "; } $query_infos .= "GROUP BY `$table`.`entities_id`"; break; } if ($result_infos = $DB->query($query_infos)) { if ($DB->numrows($result_infos)) { $found_types[$itemtype] = $item->getTypeName(1); $totalbytypes[$itemtype] = 0; //Store, for each entity, the budget spent while ($values = $DB->fetch_assoc($result_infos)) { if (!isset($entities_values[$values['entities_id']])) { $entities_values[$values['entities_id']] = 0; } if (!isset($entitiestype_values[$values['entities_id']][$itemtype])) { $entitiestype_values[$values['entities_id']][$itemtype] = 0; } $entities_values[$values['entities_id']] += $values['sumvalue']; $entitiestype_values[$values['entities_id']][$itemtype] += $values['sumvalue']; $total += $values['sumvalue']; $totalbytypes[$itemtype] += $values['sumvalue']; } } } } $budget = new self(); $budget->getFromDB($budgets_id); $colspan = count($found_types)+2; echo "
"; echo ""; echo ""; if (count($found_types)) { foreach ($found_types as $type => $typename) { echo ""; } } echo ""; echo ""; // get all entities ordered by names $allentities = getAllDatasFromTable('glpi_entities','',true, 'completename'); foreach ($allentities as $entity => $data) { if (isset($entities_values[$entity])) { echo ""; echo ""; if (count($found_types)) { foreach ($found_types as $type => $typename) { echo ""; } } echo ""; echo ""; } } if (count($found_types)) { echo ""; echo ""; foreach ($found_types as $type => $typename) { echo ""; } echo ""; echo ""; } echo ""; echo ""; echo ""; echo ""; if ($_SESSION['glpiactive_entity'] == $budget->fields['entities_id']) { echo ""; echo ""; echo ""; } echo "
".__('Total spent on the budget')."
".__('Entity')."$typename".__('Total')."
".Dropdown::getDropdownName('glpi_entities', $entity).""; $typevalue = 0; if (isset($entitiestype_values[$entity][$type])) { $typevalue = $entitiestype_values[$entity][$type]; } echo Html::formatNumber($typevalue); echo "".Html::formatNumber($entities_values[$entity])."
".__('Total').""; echo Html::formatNumber($totalbytypes[$type]); echo "".Html::formatNumber($total)."

".__('Total spent on the budget')."".Html::formatNumber($total)."
".__('Total remaining on the budget'). "".Html::formatNumber($budget->fields['value'] - $total). "
"; } }