grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026. -------------------------------------------------------------------------- */ /** @file * @brief */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /// ContractCost class /// since version 0.84 class ContractCost extends CommonDBChild { // From CommonDBChild static public $itemtype = 'Contract'; static public $items_id = 'contracts_id'; public $dohistory = true; static function getTypeName($nb=0) { return _n('Cost', 'Costs', $nb); } /** * @see CommonDBChild::prepareInputForAdd() **/ function prepareInputForAdd($input) { if (!empty($input['begin_date']) && (empty($input['end_date']) || ($input['end_date'] == 'NULL') || ($input['end_date'] < $input['begin_date']))) { $input['end_date'] = $input['begin_date']; } return parent::prepareInputForAdd($input); } /** * @see CommonDBTM::prepareInputForUpdate() **/ function prepareInputForUpdate($input) { if (!empty($input['begin_date']) && (empty($input['end_date']) || ($input['end_date'] == 'NULL') || ($input['end_date'] < $input['begin_date']))) { $input['end_date'] = $input['begin_date']; } return parent::prepareInputForUpdate($input); } /** * @see CommonGLPI::getTabNameForItem() **/ function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { // can exists for template if (($item->getType() == 'Contract') && Contract::canView()) { $nb = 0; if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable('glpi_contractcosts', "contracts_id = '".$item->getID()."'"); } return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb); } return ''; } /** * @param $item CommonGLPI object * @param $tabnum (default 1) * @param $withtemplate (default 0) */ static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { self::showForContract($item, $withtemplate); return true; } /** * @since version 0.85 * * @see CommonDBTM::getSearchOptions() **/ function getSearchOptions() { $tab = array(); $tab['common'] = __('Characteristics'); $tab[1]['table'] = $this->getTable(); $tab[1]['field'] = 'name'; $tab[1]['name'] = __('Title'); $tab[1]['searchtype'] = 'contains'; $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[16]['table'] = $this->getTable(); $tab[16]['field'] = 'comment'; $tab[16]['name'] = __('Comments'); $tab[16]['datatype'] = 'text'; $tab[12]['table'] = $this->getTable(); $tab[12]['field'] = 'begin_date'; $tab[12]['name'] = __('Begin date'); $tab[12]['datatype'] = 'datetime'; $tab[10]['table'] = $this->getTable(); $tab[10]['field'] = 'end_date'; $tab[10]['name'] = __('End date'); $tab[10]['datatype'] = 'datetime'; $tab[14]['table'] = $this->getTable(); $tab[14]['field'] = 'cost'; $tab[14]['name'] = __('Cost'); $tab[14]['datatype'] = 'decimal'; $tab[18]['table'] = 'glpi_budgets'; $tab[18]['field'] = 'name'; $tab[18]['name'] = _n('Budget', 'Budgets', 1); $tab[18]['datatype'] = 'dropdown'; $tab[80]['table'] = 'glpi_entities'; $tab[80]['field'] = 'completename'; $tab[80]['name'] = __('Entity'); $tab[80]['massiveaction'] = false; $tab[80]['datatype'] = 'dropdown'; return $tab; } /** * Duplicate all costs from a contract template to its clone * * @since version 0.84 * * @param $oldid * @param $newid **/ static function cloneContract ($oldid, $newid) { global $DB; $query = "SELECT * FROM `glpi_contractcosts` WHERE `contracts_id` = '$oldid'"; foreach ($DB->request($query) as $data) { $cd = new self(); unset($data['id']); $data['contracts_id'] = $newid; $data = Toolbox::addslashes_deep($data); $cd->add($data); } } /** * Init cost for creation based on previous cost **/ function initBasedOnPrevious() { $ticket = new Ticket(); if (!isset($this->fields['contracts_id']) || !$ticket->getFromDB($this->fields['contracts_id'])) { return false; } $lastdata = $this->getLastCostForContract($this->fields['contracts_id']); if (isset($lastdata['end_date'])) { $this->fields['begin_date'] = $lastdata['end_date']; } if (isset($lastdata['cost'])) { $this->fields['cost'] = $lastdata['cost']; } if (isset($lastdata['name'])) { $this->fields['name'] = $lastdata['name']; } if (isset($lastdata['budgets_id'])) { $this->fields['budgets_id'] = $lastdata['budgets_id']; } } /** * Get last datas for a contract * * @param $contracts_id integer ID of the contract **/ function getLastCostForContract($contracts_id) { global $DB; $query = "SELECT * FROM `".$this->getTable()."` WHERE `contracts_id` = '$contracts_id' ORDER BY 'end_date' DESC, `id` DESC"; if ($result = $DB->query($query)) { return $DB->fetch_assoc($result); } return array(); } /** * Print the contract cost form * * @param $ID integer ID of the item * @param $options array options used **/ function showForm($ID, $options=array()) { if ($ID > 0) { $this->check($ID, READ); } else { // Create item $options['contracts_id'] = $options['parent']->getField('id'); $this->check(-1, CREATE, $options); $this->initBasedOnPrevious(); } $this->showFormHeader($options); echo "
| ".self::getTypeName($DB->numrows($result))." | ||||
|---|---|---|---|---|
| ".__('Name')." | "; echo "".__('Begin date')." | "; echo "".__('End date')." | "; echo "".__('Budget')." | "; echo "".__('Cost')." | "; echo "
| "; printf(__('%1$s %2$s'), $name, Html::showToolTip($data['comment'], array('display' => false))); if ($canedit) { echo "\n\n"; } echo " | "; echo "".Html::convDate($data['begin_date'])." | "; echo "".Html::convDate($data['end_date'])." | "; echo "".Dropdown::getDropdownName('glpi_budgets', $data['budgets_id'])." | "; echo "".Html::formatNumber($data['cost'])." | "; $total += $data['cost']; echo "
| "; echo " | ".__('Total cost').' | '; echo "".Html::formatNumber($total).' | ||
| ".__('No item found')." | ||||