. * --------------------------------------------------------------------- */ 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 ""; 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 project costs * * @param $project Project object * @param $withtemplate boolean Template or basic item (default 0) * * @return void **/ static function showForProject(Project $project, $withtemplate = 0) { global $DB, $CFG_GLPI; $ID = $project->fields['id']; if (!$project->getFromDB($ID) || !$project->can($ID, READ)) { return false; } $canedit = $project->can($ID, UPDATE); echo "
"; $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => ['projects_id' => $ID], 'ORDER' => ['begin_date'] ]); $rand = mt_rand(); if ($canedit) { echo "
\n"; echo "\n"; echo "
". ""; echo __('Add a new cost')."
\n"; } $total = 0; 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'), Project::getTypeName(1), $project->getName())); 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 "
"; echo "
"; $ticketcost = TicketCost::showForObject($project); echo "
"; echo "
"; printf(__('%1$s: %2$s'), __('Total cost'), $total+$ticketcost); echo "
"; } }