. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /// ProjectCost class /// since version 0.85 class ProjectCost extends CommonDBChild { // From CommonDBChild static public $itemtype = 'Project'; static public $items_id = 'projects_id'; public $dohistory = true; static function getTypeName($nb = 0) { return _n('Cost', 'Costs', $nb); } /** * @see CommonDBChild::prepareInputForAdd() **/ function prepareInputForAdd($input) { if (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['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() == 'Project') && Project::canView()) { $nb = 0; if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable('glpi_projectcosts', ['projects_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::showForProject($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 project template to its clone * * @deprecated 9.5 * @since 0.84 * * @param $oldid * @param $newid **/ static function cloneProject ($oldid, $newid) { global $DB; Toolbox::deprecated('Use clone'); $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => ['projects_id'=> $oldid] ]); while ($data = $iterator->next()) { $cd = new self(); unset($data['id']); $data['projects_id'] = $newid; $data = self::checkTemplateEntity($data, $data['projects_id'], Project::class); $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['projects_id']) || !$ticket->getFromDB($this->fields['projects_id'])) { return false; } $lastdata = $this->getLastCostForProject($this->fields['projects_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 project * * @param $projects_id integer ID of the project **/ function getLastCostForProject($projects_id) { global $DB; $iterator = $DB->request([ 'FROM' => $this->getTable(), 'WHERE' => ['projects_id' => $projects_id], 'ORDER' => ['end_date DESC', 'id DESC'] ]); if (count($iterator)) { return $iterator->next(); } return []; } /** * Print the project 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['projects_id'] = $options['parent']->getField('id'); $this->check(-1, CREATE, $options); $this->initBasedOnPrevious(); } $this->showFormHeader($options); echo "
| ".self::getTypeName(count($iterator)). " | ||||
|---|---|---|---|---|
| ".__('Name')." | "; echo "".__('Begin date')." | "; echo "".__('End date')." | "; echo "".Budget::getTypeName(1)." | "; echo ""._n('Cost', 'Costs', 1)." | "; echo "
| "; printf(__('%1$s %2$s'), $name, Html::showToolTip($data['comment'], ['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')." | ||||