. * --------------------------------------------------------------------- */ 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); } 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); } 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); } 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 ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { self::showForContract($item, $withtemplate); return true; } function rawSearchOptions() { $tab = []; $tab[] = [ 'id' => 'common', 'name' => __('Characteristics') ]; $tab[] = [ 'id' => '1', 'table' => $this->getTable(), 'field' => 'name', 'name' => __('Title'), 'searchtype' => 'contains', 'datatype' => 'itemlink', 'massiveaction' => false, 'autocomplete' => true, ]; $tab[] = [ 'id' => '2', 'table' => $this->getTable(), 'field' => 'id', 'name' => __('ID'), 'massiveaction' => false, 'datatype' => 'number' ]; $tab[] = [ 'id' => '16', 'table' => $this->getTable(), 'field' => 'comment', 'name' => __('Comments'), 'datatype' => 'text' ]; $tab[] = [ 'id' => '12', 'table' => $this->getTable(), 'field' => 'begin_date', 'name' => __('Begin date'), 'datatype' => 'datetime' ]; $tab[] = [ 'id' => '10', 'table' => $this->getTable(), 'field' => 'end_date', 'name' => __('End date'), 'datatype' => 'datetime' ]; $tab[] = [ 'id' => '14', 'table' => $this->getTable(), 'field' => 'cost', 'name' => _n('Cost', 'Costs', 1), 'datatype' => 'decimal' ]; $tab[] = [ 'id' => '18', 'table' => 'glpi_budgets', 'field' => 'name', 'name' => Budget::getTypeName(1), 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '80', 'table' => 'glpi_entities', 'field' => 'completename', 'name' => Entity::getTypeName(1), 'massiveaction' => false, 'datatype' => 'dropdown' ]; return $tab; } /** * Duplicate all costs from a contract template to its clone * * @deprecated 9.5 * @since 0.84 * * @param $oldid * @param $newid **/ static function cloneContract($oldid, $newid) { global $DB; Toolbox::deprecated('Use clone'); $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => ['contracts_id' => $oldid] ]); while ($data = $iterator->next()) { $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; $iterator = $DB->request([ 'FROM' => $this->getTable(), 'WHERE' => ['contracts_id' => $contracts_id], 'ORDER' => ['end_date DESC', 'id DESC'] ]); if ($result = $iterator->next()) { return $result; } return []; } /** * Print the contract cost form * * @param $ID integer ID of the item * @param $options array options used **/ function showForm($ID, $options = []) { 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 ""; echo "".__('Name').""; echo ""; echo ""; Html::autocompletionTextField($this, 'name'); echo ""; echo ""._n('Cost', 'Costs', 1).""; echo ""; echo ""; echo ""; echo "".__('Begin date').""; echo ""; Html::showDateField("begin_date", ['value' => $this->fields['begin_date']]); echo ""; $rowspan = 3; echo "".__('Comments').""; echo ""; echo ""; echo "\n"; echo "".__('End date').""; echo ""; Html::showDateField("end_date", ['value' => $this->fields['end_date']]); echo ""; echo "".Budget::getTypeName(1).""; echo ""; Budget::dropdown(['value' => $this->fields["budgets_id"]]); echo ""; $this->showFormButtons($options); return true; } /** * Print the contract costs * * @param Contract $contract * @param boolean $withtemplate Template or basic item * * @return void **/ static function showForContract(Contract $contract, $withtemplate = 0) { global $DB, $CFG_GLPI; $ID = $contract->fields['id']; if (!$contract->getFromDB($ID) || !$contract->can($ID, READ)) { return; } $canedit = $contract->can($ID, UPDATE); echo "
"; $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => ['contracts_id' => $ID], 'ORDER' => 'begin_date' ]); $rand = mt_rand(); if ($canedit && ($withtemplate != 2)) { echo "
\n"; echo "\n"; echo "
". ""; echo __('Add a new cost')."
\n"; } echo ""; echo ""; if (count($iterator)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; Session::initNavigateListItems(__CLASS__, //TRANS : %1$s is the itemtype name, // %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), Contract::getTypeName(1), $contract->getName())); $total = 0; while ($data = $iterator->next()) { echo ""; $name = (empty($data['name'])? sprintf(__('%1$s (%2$s)'), $data['name'], $data['id']) : $data['name']); echo ""; echo ""; echo ""; echo ""; echo ""; $total += $data['cost']; echo ""; Session::addToNavigateListItems(__CLASS__, $data['id']); } echo ""; echo "'; echo "'; } else { echo ""; } echo "
".self::getTypeName(count($iterator))."
".__('Name')."".__('Begin date')."".__('End date')."".Budget::getTypeName(1).""._n('Cost', 'Costs', 1)."
"; printf(__('%1$s %2$s'), $name, Html::showToolTip($data['comment'], ['display' => false])); if ($canedit) { echo "\n\n"; } echo "".Html::convDate($data['begin_date'])."".Html::convDate($data['end_date'])."".Dropdown::getDropdownName('glpi_budgets', $data['budgets_id'])."".Html::formatNumber($data['cost'])."
 ".__('Total cost').'".Html::formatNumber($total).'
".__('No item found')."
"; echo "

"; } }