.
* ---------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/**
* Infocom class
**/
class Infocom extends CommonDBChild {
// From CommonDBChild
static public $itemtype = 'itemtype';
static public $items_id = 'items_id';
public $dohistory = true;
public $auto_message_on_action = false; // Link in message can't work'
static public $logs_for_parent = false;
static $rightname = 'infocom';
//Option to automatically fill dates
const ON_STATUS_CHANGE = 'STATUS';
const COPY_WARRANTY_DATE = 1;
const COPY_BUY_DATE = 2;
const COPY_ORDER_DATE = 3;
const COPY_DELIVERY_DATE = 4;
const ON_ASSET_IMPORT = 5;
/**
* Check if given object can have Infocom
*
* @since 0.85
*
* @param string|object $item an object or a string
*
* @return boolean true if $object is an object that can have Infocom
*
**/
static function canApplyOn($item) {
global $CFG_GLPI;
// All devices are subjects to infocom !
if (is_a($item, 'Item_Devices', true)) {
return true;
}
// We also allow direct items to check
if ($item instanceof CommonGLPI) {
$item = $item->getType();
}
if (in_array($item, $CFG_GLPI['infocom_types'])) {
return true;
}
return false;
}
/**
* Get all the types that can have an infocom
*
* @since 0.85
*
* @return array of the itemtypes
**/
static function getItemtypesThatCanHave() {
global $CFG_GLPI;
return array_merge($CFG_GLPI['infocom_types'],
Item_Devices::getDeviceTypes());
}
static function getTypeName($nb = 0) {
//TRANS: Always plural
return __('Financial and administrative information');
}
function post_getEmpty() {
$this->fields["alert"] = Entity::getUsedConfig("use_infocoms_alert",
$this->fields["entities_id"],
"default_infocom_alert", 0);
}
function getLogTypeID() {
return [$this->fields['itemtype'], $this->fields['items_id']];
}
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
// Can exists on template
if (Session::haveRight(self::$rightname, READ)) {
$nb = 0;
switch ($item->getType()) {
case 'Supplier' :
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForSupplier($item);
}
return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb);
default :
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = countElementsInTable('glpi_infocoms',
['itemtype' => $item->getType(),
'items_id' => $item->getID()]);
}
return self::createTabEntry(__('Management'), $nb);
}
}
return '';
}
/**
* @param $item CommonGLPI object
* @param $tabnum (default 1)
* @param $withtemplate (default 0)
**/
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
switch ($item->getType()) {
case 'Supplier' :
$item->showInfocoms();
break;
default :
self::showForItem($item, $withtemplate);
}
return true;
}
/**
* @param $item Supplier object
**/
static function countForSupplier(Supplier $item) {
return countElementsInTable(
'glpi_infocoms',
[
'suppliers_id' => $item->getField('id'),
'NOT' => ['itemtype' => ['ConsumableItem', 'CartridgeItem', 'Software']]
] + getEntitiesRestrictCriteria('glpi_infocoms', '', $_SESSION['glpiactiveentities'])
);
}
static function getSpecificValueToDisplay($field, $values, array $options = []) {
if (!is_array($values)) {
$values = [$field => $values];
}
switch ($field) {
case 'sink_type' :
return self::getAmortTypeName($values[$field]);
case 'alert' :
return self::getAlertName($values[$field]);
}
return parent::getSpecificValueToDisplay($field, $values, $options);
}
/**
* @since 0.84
*
* @param $field
* @param $name (default '')
* @param $values (default '')
* @param $options array
**/
static function getSpecificValueToSelect($field, $name = '', $values = '', array $options = []) {
if (!is_array($values)) {
$values = [$field => $values];
}
$options['display'] = false;
switch ($field) {
case "sink_type" :
return self::dropdownAmortType($name, $values[$field], false);
case "alert" :
$options['name'] = $name;
$options['value'] = $values[$field];
return self::dropdownAlert($options);
}
return parent::getSpecificValueToSelect($field, $name, $values, $options);
}
/**
* Retrieve an item from the database for a device
*
* @param string $itemtype type of the device to retrieve infocom
* @param integer $ID ID of the device to retrieve infocom
*
* @return boolean true if succeed else false
**/
function getFromDBforDevice ($itemtype, $ID) {
if ($this->getFromDBByCrit([
$this->getTable() . '.items_id' => $ID,
$this->getTable() . '.itemtype' => $itemtype
])) {
return true;
}
$this->getEmpty();
$this->fields["items_id"] = $ID;
$this->fields["itemtype"] = $itemtype;
return false;
}
function prepareInputForAdd($input) {
if (!$this->getFromDBforDevice($input['itemtype'], $input['items_id'])) {
if ($item = static::getItemFromArray(static::$itemtype, static::$items_id, $input)) {
$input['alert'] = Entity::getUsedConfig('default_infocom_alert', $item->getEntityID());
return parent::prepareInputForAdd($input);
}
}
return false;
}
/**
* Fill, if necessary, automatically some dates when status changes
*
* @param item CommonDBTM object: the item whose status have changed
* @param action_add true if object is added, false if updated (true by default)
*
* @return void
**/
static function manageDateOnStatusChange(CommonDBTM $item, $action_add = true) {
$itemtype = get_class($item);
$changes = $item->fields;
//Autofill date on item's status change ?
$infocom = new self();
$infocom->getFromDB($changes['id']);
$tmp = ['itemtype' => $itemtype,
'items_id' => $changes['id']];
$add_or_update = false;
//For each date that can be automatically filled
foreach (self::getAutoManagemendDatesFields() as $date => $date_field) {
$resp = [];
$result = Entity::getUsedConfig($date, $changes['entities_id']);
//Date must be filled if status corresponds to the one defined in the config
if (preg_match('/'.self::ON_STATUS_CHANGE.'_(.*)/', $result, $values)
&& ($values[1] == $changes['states_id'])) {
$add_or_update = true;
$tmp[$date_field] = $_SESSION["glpi_currenttime"];
}
}
//One date or more has changed
if ($add_or_update) {
if (!$infocom->getFromDBforDevice($itemtype, $changes['id'])) {
$infocom->add($tmp);
} else {
$tmp['id'] = $infocom->fields['id'];
$infocom->update($tmp);
}
}
}
/**
* Automatically manage copying one date to another is necessary
*
* @param infocoms array of item's infocom to modify
* @param field the date to modify (default '')
* @param action the action to peform (copy from another date) (default 0)
* @param params array of additional parameters needed to perform the task
*
* @return void
**/
static function autofillDates(&$infocoms = [], $field = '', $action = 0, $params = []) {
if (isset($infocoms[$field])) {
switch ($action) {
default :
case 0 :
break;
case self::COPY_WARRANTY_DATE :
if (isset($infocoms['warranty_date'])) {
$infocoms[$field] = $infocoms['warranty_date'];
}
break;
case self::COPY_BUY_DATE :
if (isset($infocoms['buy_date'])) {
$infocoms[$field] = $infocoms['buy_date'];
}
break;
case self::COPY_ORDER_DATE :
if (isset($infocoms['order_date'])) {
$infocoms[$field] = $infocoms['order_date'];
}
break;
case self::COPY_DELIVERY_DATE :
if (isset($infocoms['delivery_date'])) {
$infocoms[$field] = $infocoms['delivery_date'];
}
break;
}
}
}
/**
* Return all infocom dates that could be automaticall filled
*
* @return array with all dates (configuration field & real field)
**/
static function getAutoManagemendDatesFields() {
return ['autofill_buy_date' => 'buy_date',
'autofill_use_date' => 'use_date',
'autofill_delivery_date' => 'delivery_date',
'autofill_warranty_date' => 'warranty_date',
'autofill_order_date' => 'order_date',
'autofill_decommission_date' => 'decommission_date'];
}
function prepareInputForUpdate($input) {
//Check if one or more dates needs to be updated
foreach (self::getAutoManagemendDatesFields() as $key => $field) {
$result = Entity::getUsedConfig($key, $this->fields['entities_id']);
//Only update date if it's empty in DB. Otherwise do nothing
if (($result > 0)
&& !isset($this->fields[$field])) {
self::autofillDates($input, $field, $result);
}
}
return parent::prepareInputForUpdate($input);
}
function pre_updateInDB() {
// Clean end alert if warranty_date is after old one
// Or if duration is greater than old one
if ((isset($this->oldvalues['warranty_date'])
&& ($this->oldvalues['warranty_date'] < $this->fields['warranty_date']))
|| (isset($this->oldvalues['warranty_duration'])
&& ($this->oldvalues['warranty_duration'] < $this->fields['warranty_duration']))) {
$alert = new Alert();
$alert->clear($this->getType(), $this->fields['id'], Alert::END);
}
// Check budgets link validity
if ((in_array('budgets_id', $this->updates)
|| in_array('buy_date', $this->updates))
&& $this->fields['budgets_id']
&& ($budget = getItemForItemtype('Budget'))
&& $budget->getFromDB($this->fields['budgets_id'])) {
if ((!is_null($budget->fields['begin_date'])
&& $this->fields['buy_date'] < $budget->fields['begin_date'])
|| (!is_null($budget->fields['end_date'])
&& ($this->fields['buy_date'] > $budget->fields['end_date']))) {
$msg = sprintf(__('Purchase date incompatible with the associated budget. %1$s not in budget period: %2$s / %3$s'),
Html::convDate($this->fields['buy_date']),
Html::convDate($budget->fields['begin_date']),
Html::convDate($budget->fields['end_date']));
Session::addMessageAfterRedirect($msg, false, ERROR);
}
}
}
/**
* @since 0.84
**/
function cleanDBonPurge() {
$class = new Alert();
$class->cleanDBonItemDelete($this->getType(), $this->fields['id']);
}
/**
* @param $name
**/
static function cronInfo($name) {
return ['description' => __('Send alarms on financial and administrative information')];
}
/**
* Cron action on infocom : alert on expired warranty
*
* @param CronTask $task to log, if NULL use display (default NULL)
*
* @return integer 0 : nothing to do 1 : done with success
**/
static function cronInfocom($task = null) {
global $DB, $CFG_GLPI;
if (!$CFG_GLPI["use_notifications"]) {
return 0;
}
$message = [];
$cron_status = 0;
$items_infos = [];
$items_messages = [];
foreach (Entity::getEntitiesToNotify('use_infocoms_alert') as $entity => $value) {
$before = Entity::getUsedConfig('send_infocoms_alert_before_delay', $entity);
$table = self::getTable();
$iterator = $DB->request([
'SELECT' => "$table.*",
'FROM' => $table,
'LEFT JOIN' => [
'glpi_alerts' => [
'ON' => [
'glpi_alerts' => 'items_id',
$table => 'id', [
'AND' => [
'glpi_alerts.itemtype' => self::getType(),
'glpi_alerts.type' => Alert::END
]
]
]
]
],
'WHERE' => [
new \QueryExpression(
'(' . $DB->quoteName('glpi_infocoms.alert') . ' & ' . pow(2, Alert::END). ') > 0'
),
"$table.entities_id" => $entity,
"$table.warranty_duration" => ['>', 0],
'NOT' => ["$table.warranty_date" => null],
new \QueryExpression(
'DATEDIFF(ADDDATE(' . $DB->quoteName('glpi_infocoms.warranty_date') . ', INTERVAL (' .
$DB->quoteName('glpi_infocoms.warranty_duration') . ') MONTH), CURDATE() ) <= ' .
$DB->quoteValue($before)
),
'glpi_alerts.date' => null
]
]);
while ($data = $iterator->next()) {
if ($item_infocom = getItemForItemtype($data["itemtype"])) {
if ($item_infocom->getFromDB($data["items_id"])) {
$entity = $data['entities_id'];
$warranty = self::getWarrantyExpir($data["warranty_date"], $data["warranty_duration"]);
//TRANS: %1$s is a type, %2$s is a name (used in croninfocom)
$name = sprintf(__('%1$s - %2$s'), $item_infocom->getTypeName(1),
$item_infocom->getName());
//TRANS: %1$s is the warranty end date and %2$s the name of the item
$message = sprintf(__('Item reaching the end of warranty on %1$s: %2$s'),
$warranty, $name)."
";
$data['warrantyexpiration'] = $warranty;
$data['item_name'] = $item_infocom->getName();
$items_infos[$entity][$data['id']] = $data;
if (!isset($items_messages[$entity])) {
$items_messages[$entity] = __('No item reaching the end of warranty.')."
";
}
$items_messages[$entity] .= $message;
}
}
}
}
foreach ($items_infos as $entity => $items) {
if (NotificationEvent::raiseEvent("alert", new self(), ['entities_id' => $entity,
'items' => $items])) {
$message = $items_messages[$entity];
$cron_status = 1;
if ($task) {
$task->log(sprintf(__('%1$s: %2$s')."\n",
Dropdown::getDropdownName("glpi_entities", $entity), $message));
$task->addVolume(1);
} else {
Session::addMessageAfterRedirect(sprintf(__('%1$s: %2$s'),
Dropdown::getDropdownName("glpi_entities",
$entity),
$message));
}
$alert = new Alert();
$input["itemtype"] = 'Infocom';
$input["type"] = Alert::END;
foreach ($items as $id => $item) {
$input["items_id"] = $id;
$alert->add($input);
unset($alert->fields['id']);
}
} else {
$entityname = Dropdown::getDropdownName('glpi_entities', $entity);
//TRANS: %s is entity name
$msg = sprintf(__('%1$s: %2$s'), $entityname, __('send infocom alert failed'));
if ($task) {
$task->log($msg);
} else {
Session::addMessageAfterRedirect($msg, false, ERROR);
}
}
}
return $cron_status;
}
/**
* Get the possible value for infocom alert
*
* @since 0.84 (before in alert.class)
*
* @param integer|string|null $val if not set, ask for all values, else for 1 value (default NULL)
*
* @return array|string
**/
static function getAlertName($val = null) {
$tmp[0] = Dropdown::EMPTY_VALUE;
$tmp[pow(2, Alert::END)] = __('Warranty expiration date');
if (is_null($val)) {
return $tmp;
}
// Default value for display
$tmp[0] = ' ';
if (isset($tmp[$val])) {
return $tmp[$val];
}
// If not set and is a string return value
if (is_string($val)) {
return $val;
}
return NOT_AVAILABLE;
}
/**
* @param $options array
**/
static function dropdownAlert($options) {
$p['name'] = 'alert';
$p['value'] = 0;
$p['display'] = true;
$p['inherit_parent'] = false;
if (count($options)) {
foreach ($options as $key => $val) {
$p[$key] = $val;
}
}
$tab = [];
if ($p['inherit_parent']) {
$tab[Entity::CONFIG_PARENT] = __('Inheritance of the parent entity');
}
$tab += self::getAlertName();
return Dropdown::showFromArray($p['name'], $tab, $p);
}
/**
* Dropdown of amortissement type for infocoms
*
* @param string $name select name
* @param integer $value default value (default 0)
* @param boolean $display display or get string (true by default)
**/
static function dropdownAmortType($name, $value = 0, $display = true) {
$values = [2 => __('Linear'),
1 => __('Decreasing')];
return Dropdown::showFromArray($name, $values,
['value' => $value,
'display' => $display,
'display_emptychoice' => true]);
}
/**
* Get amortissement type name for infocoms
*
* @param integer $value status ID
**/
static function getAmortTypeName($value) {
switch ($value) {
case 2 :
return __('Linear');
case 1 :
return __('Decreasing');
case 0 :
return " ";
}
}
/**
* Calculate TCO and TCO by month for an item
*
* @param string|number $ticket_tco Tco part of tickets
* @param number $value
* @param string $date_achat (default '')
*
* @return float
**/
static function showTco($ticket_tco, $value, $date_achat = "") {
if ($ticket_tco == NOT_AVAILABLE) {
return '-';
}
// Affiche le TCO ou le TCO mensuel pour un mat??riel
$totalcost = $ticket_tco;
if ($date_achat) { // on veut donc le TCO mensuel
// just to avoid IDE warning
$date_Y = $date_m = $date_d = 0;
sscanf($date_achat, "%4s-%2s-%2s", $date_Y, $date_m, $date_d);
$timestamp2 = mktime(0, 0, 0, $date_m, $date_d, $date_Y);
$timestamp = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$diff = floor(($timestamp - $timestamp2) / (MONTH_TIMESTAMP)); // Mois d'utilisation
if ($diff) {
return Html::formatNumber((($totalcost+$value)/$diff)); // TCO mensuel
}
return "";
}
return Html::formatNumber(($totalcost+$value)); // TCO
}// fin showTCO
/**
* Show infocom link to display modal
*
* @param $itemtype integer item type
* @param $device_id integer item ID
*
* @return float
**/
static function showDisplayLink($itemtype, $device_id) {
global $DB, $CFG_GLPI;
if (!Session::haveRight(self::$rightname, READ)
|| !($item = getItemForItemtype($itemtype))) {
return false;
}
$result = $DB->request([
'COUNT' => 'cpt',
'FROM' => 'glpi_infocoms',
'WHERE' => [
'itemtype' => $itemtype,
'items_id' => $device_id
]
])->next();
$add = "add";
$text = __('Add');
if ($result['cpt'] > 0) {
$add = "";
$text = _x('button', 'Show');
} else if (!Infocom::canUpdate()) {
return false;
}
if ($item->canView()) {
echo "
";
Ajax::createIframeModalWindow('infocom'.$itemtype.$device_id,
Infocom::getFormURL().
"?itemtype=$itemtype&items_id=$device_id",
['height' => 600]);
}
}
/**
* Calculate amortization values
*
* @param number $value Purchase value
* @param number $duration Amortise duration
* @param string $fiscaldate Begin of fiscal excercise
* @param number $buydate Buy date
* @param number $usedate Date of use
*
* @return array|boolean
*/
static public function linearAmortise($value, $duration, $fiscaldate, $buydate = '', $usedate = '') {
//Set timezone to UTC; see https://stackoverflow.com/a/40358744
$TZ = 'UTC';
try {
if ($fiscaldate == '') {
throw new \RuntimeException('Empty date');
}
$fiscaldate = new \DateTime($fiscaldate, new DateTimeZone($TZ));
} catch (\Exception $e) {
Session::addMessageAfterRedirect(
__('Please fill you fiscal year date in preferences.'),
false,
ERROR
);
return false;
}
//get begin date. Work on use date if provided.
try {
if ($buydate == '' && $usedate == '') {
throw new \RuntimeException('Empty date');
}
if ($usedate != '') {
$usedate = new \DateTime($usedate, new DateTimeZone($TZ));
} else {
$usedate = new \DateTime($buydate, new DateTimeZone($TZ));
}
} catch (\Exception $e) {
Session::addMessageAfterRedirect(
__('Please fill either buy or use date in preferences.'),
false,
ERROR
);
return false;
}
$now = new \DateTime('now', new DateTimeZone($TZ));
$elapsed_years = $now->format('Y') - $usedate->format('Y');
$annuity = $value * (1 / $duration);
$years = [];
for ($i = 0; $i <= $elapsed_years; ++$i) {
$begin_value = $value;
$current_annuity = $annuity;
$fiscal_end = new \DateTime(
$fiscaldate->format('d-m-') . ($usedate->format('Y') + $i),
new DateTimeZone($TZ)
);
if ($i == 0) {
//first year, calculate prorata
if ($fiscal_end < $usedate) {
$fiscal_end->modify('+1 year');
}
$days = $fiscal_end->diff($usedate);
$days = $days->format('%m') * 30 + $days->format('%d');
$current_annuity = $annuity * $days / 360;
} else if ($i == $duration) {
$current_annuity = $value;
}
if ($i > $duration) {
$value = 0;
$current_annuity = 0;
} else {
//calculate annuity
//full year case
$value -= $current_annuity;
}
$years[$usedate->format('Y') + $i] = [
'start_value' => (double)$begin_value,
'value' => $value,
'annuity' => $current_annuity
];
}
return $years;
}
/**
* Maps new amortise format to old one...
* To not rewrite all the old method.
*
* @param array $values New format amortise values
* @param boolean $current True to get only current year, false to get the whole array
*
* @return array|double
*/
public static function mapOldAmortiseFormat($values, $current = true) {
if ($current === true) {
return $values[date('Y')]['value'];
}
$old = [
'annee' => [],
'annuite' => [],
'vcnetdeb' => [],
'vcnetfin' => []
];
foreach ($values as $year => $value) {
$old['annee'][] = $year;
$old['annuite'][] = $value['annuity'];
$old['vcnetdeb'][] = $value['start_value'];
$old['vcnetfin'][] = $value['value'];
}
return $old;
}
/**
* Calculate amortissement for an item
*
* @param integer $type_amort type d'amortisssment "lineaire=2" ou "degressif=1"
* @param number $va valeur d'acquisition
* @param number $duree duree d'amortissement
* @param number $coef coefficient d'amortissement
* @param string $date_achat Date d'achat
* @param string $date_use Date d'utilisation
* @param string $date_tax date du debut de l'annee fiscale
* @param string $view "n" pour l'annee en cours ou "all" pour le tableau complet (default 'n')
*
* @return float|array
**/
static function Amort($type_amort, $va, $duree, $coef, $date_achat, $date_use, $date_tax,
$view = "n") {
// By Jean-Mathieu Doleans qui s'est un peu pris le chou :p
// Attention date mise en service/dateachat ->amort lineaire et $prorata en jour !!
// amort degressif au prorata du nombre de mois.
// Son point de depart est le 1er jour du mois d'acquisition et non date de mise en service
if ($type_amort == "2") {
$values = self::linearAmortise($va, $duree, $date_tax, $date_achat, $date_use);
if ($values == false) {
return '-';
}
return self::mapOldAmortiseFormat($values, $view != 'all');
}
$prorata = 0;
$ecartfinmoiscourant = 0;
$ecartmoisexercice = 0;
$date_Y = $date_m = $date_d = $date_H = $date_i = $date_s = 0;
sscanf($date_achat, "%4s-%2s-%2s %2s:%2s:%2s",
$date_Y, $date_m, $date_d,
$date_H, $date_i, $date_s); // un traitement sur la date mysql pour recuperer l'annee
// un traitement sur la date mysql pour les infos necessaires
$date_Y2 = $date_m2 = $date_d2 = $date_H2 = $date_i2 = $date_s2=0;
sscanf($date_tax, "%4s-%2s-%2s %2s:%2s:%2s",
$date_Y2, $date_m2, $date_d2,
$date_H2, $date_i2, $date_s2);
$date_Y2 = date("Y");
switch ($type_amort) {
case "1" :
//########################### Calcul amortissement degressif ###########################
if (($va > 0)
&& ($duree > 0)
&& ($coef > 1)
&& !empty($date_achat)) {
//## calcul du prorata temporis en mois ##
// si l'annee fiscale debute au dela de l'annee courante
if ($date_m > $date_m2) {
$date_m2 = $date_m2+12;
}
$ecartmois = ($date_m2-$date_m)+1; // calcul ecart entre mois d'acquisition
// et debut annee fiscale
$prorata = $ecartfinmoiscourant+$ecartmois-$ecartmoisexercice;
// calcul tableau d'amortissement ##
$txlineaire = (100/$duree); // calcul du taux lineaire virtuel
$txdegressif = $txlineaire*$coef; // calcul du taux degressif
$dureelineaire = (int) (100/$txdegressif); // calcul de la duree de l'amortissement
// en mode lineaire
$dureedegressif = $duree-$dureelineaire; // calcul de la duree de l'amortissement
// en mode degressif
$mrt = $va;
// amortissement degressif pour les premieres annees
for ($i=1; $i<=$dureedegressif; $i++) {
$tab['annee'][$i] = $date_Y+$i-1;
$tab['vcnetdeb'][$i] = $mrt; // Pour chaque annee on calcule la valeur comptable nette
// de debut d'exercice
$tab['annuite'][$i] = $tab['vcnetdeb'][$i]*$txdegressif/100;
$tab['vcnetfin'][$i] = $mrt - $tab['annuite'][$i]; //Pour chaque annee on calcule la valeur
//comptable nette de fin d'exercice
// calcul de la premiere annuite si prorata temporis
if ($prorata > 0) {
$tab['annuite'][1] = ($va*$txdegressif/100)*($prorata/12);
$tab['vcnetfin'][1] = $va - $tab['annuite'][1];
}
$mrt = $tab['vcnetfin'][$i];
}
// amortissement en lineaire pour les derneres annees
if ($dureelineaire != 0) {
$txlineaire = (100/$dureelineaire); // calcul du taux lineaire
} else {
$txlineaire = 100;
}
$annuite = ($tab['vcnetfin'][$dureedegressif]*$txlineaire)/100; // calcul de l'annuite
$mrt = $tab['vcnetfin'][$dureedegressif];
for ($i=$dureedegressif+1; $i<=$dureedegressif+$dureelineaire; $i++) {
$tab['annee'][$i] = $date_Y+$i-1;
$tab['annuite'][$i] = $annuite;
$tab['vcnetdeb'][$i] = $mrt; // Pour chaque annee on calcule la valeur comptable nette
// de debut d'exercice
$tab['vcnetfin'][$i] = abs(($mrt - $annuite)); // Pour chaque annee on calcule la valeur
// comptable nette de fin d'exercice
$mrt = $tab['vcnetfin'][$i];
}
// calcul de la derniere annuite si prorata temporis
if ($prorata > 0) {
$tab['annuite'][$duree] = $tab['vcnetdeb'][$duree];
if (isset($tab['vcnetfin'][$duree-1])) {
$tab['vcnetfin'][$duree] = ($tab['vcnetfin'][$duree-1] - $tab['annuite'][$duree]);
} else {
$tab['vcnetfin'][$duree] = 0;
}
}
} else {
return "-";
}
break;
default :
return "-";
}
// le return
if ($view == "all") {
// on retourne le tableau complet
return $tab;
}
// on retourne juste la valeur residuelle
// si on ne trouve pas l'annee en cours dans le tableau d'amortissement dans le tableau,
// le materiel est amorti
if (!array_search(date("Y"), $tab["annee"])) {
$vnc = 0;
} else if (mktime(0, 0, 0, $date_m2, $date_d2, date("Y"))
- mktime(0, 0, 0, date("m"), date("d"), date("Y")) < 0) {
// on a depasse la fin d'exercice de l'annee en cours
//on prend la valeur residuelle de l'annee en cours
$vnc = $tab["vcnetfin"][array_search(date("Y"), $tab["annee"])];
} else {
// on se situe avant la fin d'exercice
// on prend la valeur residuelle de l'annee n-1
$vnc = $tab["vcnetdeb"][array_search(date("Y"), $tab["annee"])];
}
return $vnc;
}
/**
* Show Infocom form for an item (not a standard showForm)
*
* @param $item CommonDBTM object
* @param $withtemplate integer template or basic item (default 0)
**/
static function showForItem(CommonDBTM $item, $withtemplate = 0) {
global $CFG_GLPI;
// Show Infocom or blank form
if (!self::canView()) {
return false;
}
if (!$item) {
echo "
| "; echo sprintf(__('%1$s - %2$s'), $item->getTypeName(1), $item->getName())." |
|---|
| "; Html::showSimpleForm(Infocom::getFormURL(), 'add', __('Enable the financial and administrative information'), ['itemtype' => $item->getType(), 'items_id' => $dev_ID]); echo " |