dolibarr  17.0.4
assetdepreciationoptions.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2021 Open-Dsi <support@open-dsi.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25 
30 {
34  public $table_element = '';
35 
69  public $fields = array();
70 
75  public $deprecation_options_fields = array(
76  'economic' => array(
77  'label' => 'AssetDepreciationOptionEconomic',
78  'table' => 'asset_depreciation_options_economic',
79  'fields' => array(
80  'depreciation_type' => array('type'=>'smallint', 'label'=>'AssetDepreciationOptionDepreciationType', 'enabled'=>'1', 'position'=>10, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'arrayofkeyval'=>array('0'=>'AssetDepreciationOptionDepreciationTypeLinear', '1'=>'AssetDepreciationOptionDepreciationTypeDegressive', '2'=>'AssetDepreciationOptionDepreciationTypeExceptional'), 'validate'=>'1',),
81  'degressive_coefficient' => array('type'=>'double(24,8)', 'label'=>'AssetDepreciationOptionDegressiveRate', 'enabled'=>'1', 'position'=>20, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1','enabled_field' => 'economic:depreciation_type:1'),
82  'duration' => array('type'=>'integer', 'label'=>'AssetDepreciationOptionDuration', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1',),
83  'duration_type' => array('type'=>'smallint', 'label'=>'AssetDepreciationOptionDurationType', 'enabled'=>'1', 'position'=>40, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'arrayofkeyval'=>array('0'=>'AssetDepreciationOptionDurationTypeAnnual', '1'=>'AssetDepreciationOptionDurationTypeMonthly'/*, '2'=>'AssetDepreciationOptionDurationTypeDaily'*/), 'validate'=>'1',),
84  'rate' => array('type'=>'double(24,8)', 'label'=>'AssetDepreciationOptionRate', 'enabled'=>'1', 'position'=>50, 'visible'=>3, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1', 'computed' => '$object->asset_depreciation_options->getRate("economic")',),
85  'accelerated_depreciation_option' => array('type'=>'boolean', 'label'=>'AssetDepreciationOptionAcceleratedDepreciation', 'enabled'=>'1', 'position'=>60, 'column_break' => true, 'notnull'=>0, 'default'=>'0', 'visible'=>1, 'validate'=>'1',),
86  'amount_base_depreciation_ht' => array('type'=>'price', 'label'=>'AssetDepreciationOptionAmountBaseDepreciationHT', 'enabled'=>'isset($object)&&get_class($object)=="Asset"', 'only_on_asset'=>1, 'position'=>90, 'notnull'=>0, 'required'=>1, 'visible'=>1, 'default'=>'$object->reversal_amount_ht > 0 ? $object->reversal_amount_ht : $object->acquisition_value_ht', 'isameasure'=>'1', 'validate'=>'1',),
87  'amount_base_deductible_ht' => array('type'=>'price', 'label'=>'AssetDepreciationOptionAmountBaseDeductibleHT', 'enabled'=>'isset($object)&&get_class($object)=="Asset"', 'only_on_asset'=>1, 'position'=>100, 'notnull'=>0, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1',),
88  'total_amount_last_depreciation_ht' => array('type'=>'price', 'label'=>'AssetDepreciationOptionTotalAmountLastDepreciationHT', 'enabled'=>'isset($object)&&get_class($object)=="Asset"', 'only_on_asset'=>1, 'position'=>110, 'noteditable'=> 1, 'notnull'=>0, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1',),
89  ),
90  ),
91  'accelerated_depreciation' => array(
92  'label' => 'AssetDepreciationOptionAcceleratedDepreciation',
93  'table' => 'asset_depreciation_options_fiscal',
94  'enabled_field' => 'economic:accelerated_depreciation_option:1',
95  'fields' => array(
96  'depreciation_type' => array('type'=>'smallint', 'label'=>'AssetDepreciationOptionDepreciationType', 'enabled'=>'1', 'position'=>10, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'arrayofkeyval'=>array('0'=>'AssetDepreciationOptionDepreciationTypeLinear', '1'=>'AssetDepreciationOptionDepreciationTypeDegressive', '2'=>'AssetDepreciationOptionDepreciationTypeExceptional'), 'validate'=>'1',),
97  'degressive_coefficient' => array('type'=>'double(24,8)', 'label'=>'AssetDepreciationOptionDegressiveRate', 'enabled'=>'1', 'position'=>20, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1','enabled_field' => 'accelerated_depreciation:depreciation_type:1'),
98  'duration' => array('type'=>'integer', 'label'=>'AssetDepreciationOptionDuration', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1',),
99  'duration_type' => array('type'=>'smallint', 'label'=>'AssetDepreciationOptionDurationType', 'enabled'=>'1', 'position'=>40, 'notnull'=>1, 'visible'=>1, 'default'=>'0', 'arrayofkeyval'=>array('0'=>'AssetDepreciationOptionDurationTypeAnnual', '1'=>'AssetDepreciationOptionDurationTypeMonthly'/*, '2'=>'AssetDepreciationOptionDurationTypeDaily'*/), 'validate'=>'1',),
100  'rate' => array('type'=>'double(24,8)', 'label'=>'AssetDepreciationOptionRate', 'enabled'=>'1', 'position'=>50, 'visible'=>3, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1', 'computed' => '$object->asset_depreciation_options->getRate("accelerated_depreciation")',),
101  'amount_base_depreciation_ht' => array('type'=>'price', 'label'=>'AssetDepreciationOptionAmountBaseDepreciationHT', 'enabled'=>'isset($object)&&get_class($object)=="Asset"', 'only_on_asset'=>1, 'position'=>80, 'column_break' => true, 'notnull'=>0, 'required'=>1, 'visible'=>1, 'default'=>'$object->reversal_amount_ht > 0 ? $object->reversal_amount_ht : $object->acquisition_value_ht', 'isameasure'=>'1', 'validate'=>'1',),
102  'amount_base_deductible_ht' => array('type'=>'price', 'label'=>'AssetDepreciationOptionAmountBaseDeductibleHT', 'enabled'=>'isset($object)&&get_class($object)=="Asset"', 'only_on_asset'=>1, 'position'=>90, 'notnull'=>0, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1',),
103  'total_amount_last_depreciation_ht' => array('type'=>'price', 'label'=>'AssetDepreciationOptionTotalAmountLastDepreciationHT', 'enabled'=>'isset($object)&&get_class($object)=="Asset"', 'only_on_asset'=>1, 'position'=>100, 'noteditable'=> 1, 'notnull'=>0, 'visible'=>1, 'default'=>'0', 'isameasure'=>'1', 'validate'=>'1',),
104  ),
105  ),
106  );
107  public $fk_asset;
108  public $fk_asset_model;
109  public $tms;
110  public $fk_user_modif;
111 
115  public $deprecation_options = array();
116 
122  public function __construct(DoliDB $db)
123  {
124  global $langs;
125  $this->db = $db;
126 
127  // Translate some data of arrayofkeyval
128  if (is_object($langs)) {
129  foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
130  if (!empty($mode_info['fields']) && is_array($mode_info['fields'])) {
131  foreach ($mode_info['fields'] as $field_key => $field_info) {
132  if (!empty($field_info['arrayofkeyval']) && is_array($field_info['arrayofkeyval'])) {
133  foreach ($field_info['arrayofkeyval'] as $key => $val) {
134  $this->deprecation_options_fields[$mode_key]['fields'][$field_key]['arrayofkeyval'][$key] = $langs->trans($val);
135  }
136  }
137  }
138  }
139  }
140  }
141  }
142 
151  public function setInfosForMode($mode, $class_type = 0, $all_field = false)
152  {
153  // Clean parameters
154  $mode = strtolower(trim($mode));
155 
156  if (!empty($this->deprecation_options_fields[$mode])) {
157  $this->table_element = $this->deprecation_options_fields[$mode]['table'];
158  $this->fields = $this->deprecation_options_fields[$mode]['fields'];
159  foreach ($this->fields as $field_key => $field_info) {
160  if ((!empty($field_info['computed']) && !$all_field) || (!empty($field_info['only_on_asset']) && !empty($class_type))) {
161  unset($this->fields[$field_key]);
162  continue;
163  }
164 
165  // Unset required option (notnull) if field disabled
166  if (!empty($field_info['enabled_field'])) {
167  $info = explode(':', $field_info['enabled_field']);
168  if ($this->deprecation_options[$info[0]][$info[1]] != $info[2] && isset($this->fields[$field_key]['notnull'])) {
169  unset($this->fields[$field_key]['notnull']);
170  }
171  }
172  // Set value of the field in the object (for createCommon and setDeprecationOptionsFromPost functions)
173  $this->{$field_key} = $this->deprecation_options[$mode][$field_key];
174  }
175 
176  $this->fields['rowid'] = array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id");
177  if (empty($class_type)) {
178  $this->fields['fk_asset'] = array('type' => 'integer:Asset:asset/class/asset.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'label' => 'Asset', 'enabled' => '1', 'position' => 0, 'notnull' => 0, 'visible' => 0, 'index' => 1, 'validate' => '1',);
179  } else {
180  $this->fields['fk_asset_model'] = array('type' => 'integer:AssetModel:asset/class/assetmodel.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'label' => 'AssetModel', 'enabled' => '1', 'position' => 0, 'notnull' => 0, 'visible' => 0, 'index' => 1, 'validate' => '1',);
181  }
182  $this->fields['tms'] = array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => '1', 'position' => 501, 'notnull' => 0, 'visible' => 0,);
183  $this->fields['fk_user_modif'] = array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => '1', 'position' => 511, 'notnull' => -1, 'visible' => 0,);
184  }
185 
186  return 1;
187  }
188 
195  public function setDeprecationOptionsFromPost($class_type = 0)
196  {
197  global $conf, $langs;
198 
199  $error = 0;
200 
201  $deprecation_options = array();
202  foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
203  $this->setInfosForMode($mode_key, $class_type);
204 
205  foreach ($mode_info['fields'] as $field_key => $field_info) {
206  if (!empty($field_info['computed'])) {
207  continue;
208  }
209 
210  $html_name = $mode_key . '_' . $field_key;
211  if ($field_info['type'] == 'duration') {
212  if (GETPOST($html_name . 'hour') == '' && GETPOST($html_name . 'min') == '') {
213  continue; // The field was not submited to be saved
214  }
215  } else {
216  if (!GETPOSTISSET($html_name)) {
217  continue; // The field was not submited to be saved
218  }
219  }
220  // Ignore special fields
221  if (in_array($field_key, array('rowid', 'entity', 'import_key'))) {
222  continue;
223  }
224  if (in_array($field_key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
225  if (!in_array(abs($field_info['visible']), array(1, 3))) {
226  continue; // Only 1 and 3 that are case to create
227  }
228  }
229 
230  // Set value to insert
231  if (in_array($field_info['type'], array('text', 'html'))) {
232  $value = GETPOST($html_name, 'restricthtml');
233  } elseif ($field_info['type'] == 'date') {
234  $value = dol_mktime(12, 0, 0, GETPOST($html_name . 'month', 'int'), GETPOST($html_name . 'day', 'int'), GETPOST($html_name . 'year', 'int')); // for date without hour, we use gmt
235  } elseif ($field_info['type'] == 'datetime') {
236  $value = dol_mktime(GETPOST($html_name . 'hour', 'int'), GETPOST($html_name . 'min', 'int'), GETPOST($html_name . 'sec', 'int'), GETPOST($html_name . 'month', 'int'), GETPOST($html_name . 'day', 'int'), GETPOST($html_name . 'year', 'int'), 'tzuserrel');
237  } elseif ($field_info['type'] == 'duration') {
238  $value = 60 * 60 * GETPOST($html_name . 'hour', 'int') + 60 * GETPOST($html_name . 'min', 'int');
239  } elseif (preg_match('/^(integer|price|real|double)/', $field_info['type'])) {
240  $value = price2num(GETPOST($html_name, 'alphanohtml')); // To fix decimal separator according to lang setup
241  } elseif ($field_info['type'] == 'boolean') {
242  $value = ((GETPOST($html_name) == '1' || GETPOST($html_name) == 'on') ? 1 : 0);
243  } elseif ($field_info['type'] == 'reference') {
244  // todo to check
245  $tmparraykey = array(); //array_keys($object->param_list);
246  $value = $tmparraykey[GETPOST($html_name)] . ',' . GETPOST($html_name . '2');
247  } else {
248  if ($field_key == 'lang') {
249  $value = GETPOST($html_name, 'aZ09') ? GETPOST($html_name, 'aZ09') : "";
250  } else {
251  $value = GETPOST($html_name, 'alphanohtml');
252  }
253  }
254  if (preg_match('/^integer:/i', $field_info['type']) && $value == '-1') {
255  $value = ''; // This is an implicit foreign key field
256  }
257  if (!empty($field_info['foreignkey']) && $value == '-1') {
258  $value = ''; // This is an explicit foreign key field
259  }
260 
261  //var_dump($field_key.' '.$value.' '.$field_info['type']);
262  $field_value = $value;
263  if ($field_info['notnull'] > 0 && $field_value == '' && !is_null($field_info['default']) && $field_info['default'] == '(PROV)') {
264  $field_value = '(PROV)';
265  } elseif ((!empty($field_info['required']) || $field_info['notnull'] > 0) && $field_value == '' && !empty($field_info['default'])) {
266  $field_value = dol_eval($field_info['default'], 1);
267  }
268  if ($field_info['notnull'] > 0 && $field_value == '' && is_null($field_info['default'])) {
269  $error++;
270  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($field_info['label'])), null, 'errors');
271  }
272  $deprecation_options[$mode_key][$field_key] = $field_value;
273 
274  // Validation of fields values
275  if ($conf->global->MAIN_FEATURE_LEVEL >= 2 || !empty($conf->global->MAIN_ACTIVATE_VALIDATION_RESULT)) {
276  if (!$error && !empty($field_info['validate']) && is_callable(array($this, 'validateField'))) {
277  if (!$this->validateField($mode_info['fields'], $field_key, $value)) {
278  $error++;
279  }
280  }
281  }
282  }
283  }
284  // Unset not enabled modes
285  foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
286  if (!empty($mode_info['enabled_field'])) {
287  $info = explode(':', $mode_info['enabled_field']);
288  if ($deprecation_options[$info[0]][$info[1]] != $info[2]) {
289  unset($deprecation_options[$info[0]][$info[1]]);
290  }
291  }
292  }
293  $this->deprecation_options = $deprecation_options;
294 
295  if ($error) {
296  return -1;
297  } else {
298  return 1;
299  }
300  }
301 
309  public function fetchDeprecationOptions($asset_id = 0, $asset_model_id = 0)
310  {
311  global $langs, $hookmanager;
312  dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
313 
314  $error = 0;
315  $this->errors = array();
316  $this->deprecation_options = array();
317 
318  // Clean parameters
319  $asset_id = $asset_id > 0 ? $asset_id : 0;
320  $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
321 
322  if (!is_object($hookmanager)) {
323  require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
324  $hookmanager = new HookManager($this->db);
325  }
326 
327  $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
328  $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
329  $reshook = $hookmanager->executeHooks('fetchDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
330  if (!empty($reshook)) {
331  return $reshook;
332  }
333 
334  // Check parameters
335  if (empty($asset_id) && empty($asset_model_id)) {
336  $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
337  $error++;
338  }
339  if ($error) {
340  dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
341  return -1;
342  }
343 
344  $class_type = $asset_id > 0 ? 0 : 1;
345  $deprecation_options = array();
346  foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
347  $this->setInfosForMode($mode_key, $class_type);
348 
349  $result = $this->fetchCommon(0, '', " AND " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id));
350  if ($result < 0) {
351  $this->errors = array_merge(array($langs->trans('AssetErrorFetchDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
352  $error++;
353  } elseif ($result > 0) {
354  foreach ($this->fields as $field_key => $field_info) {
355  if (in_array($field_key, array('rowid', 'fk_asset', 'fk_asset_model', 'tms', 'fk_user_modif'))) continue;
356  $deprecation_options[$mode_key][$field_key] = $this->{$field_key};
357  }
358  }
359  }
360  // Unset not enabled modes
361  foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
362  if (!empty($mode_info['enabled_field'])) {
363  $info = explode(':', $mode_info['enabled_field']);
364  if ($deprecation_options[$info[0]][$info[1]] != $info[2]) {
365  unset($deprecation_options[$info[0]][$info[1]]);
366  }
367  }
368  }
369 
370  if ($error) {
371  dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
372  return -1;
373  } else {
374  $this->deprecation_options = $deprecation_options;
375  return 1;
376  }
377  }
378 
385  public function getGeneralDepreciationInfoForMode($mode)
386  {
387  global $hookmanager;
388  dol_syslog(__METHOD__ . " mode=$mode");
389 
390  $this->errors = array();
391 
392  // Clean parameters
393  $mode = strtolower(trim($mode));
394 
395  if (!is_object($hookmanager)) {
396  require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
397  $hookmanager = new HookManager($this->db);
398  }
399 
400  $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
401  $parameters = array('mode' => $mode);
402  $reshook = $hookmanager->executeHooks('getGeneralDepreciationInfoForMode', $parameters, $this); // Note that $action and $object may have been modified by some hooks
403  if ($reshook < 0) {
404  return $reshook;
405  } elseif ($reshook > 0) {
406  return $hookmanager->resArray;
407  }
408 
409  $duration_type_list = $this->deprecation_options_fields[$mode]['fields']['duration_type']['arrayofkeyval'];
410 
411  return array(
412  'base_depreciation_ht' => $this->deprecation_options[$mode]['amount_base_depreciation_ht'],
413  'duration' => $this->deprecation_options[$mode]['duration'],
414  'duration_type' => $duration_type_list[$this->deprecation_options[$mode]['duration_type']],
415  'rate' => $this->getRate($mode),
416  );
417  }
418 
428  public function updateDeprecationOptions($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
429  {
430  global $langs, $hookmanager;
431  dol_syslog(__METHOD__ . " user_id={$user->id}, asset_id=$asset_id, asset_model_id=$asset_model_id, notrigger=$notrigger");
432 
433  $error = 0;
434  $this->errors = array();
435 
436  // Clean parameters
437  $asset_id = $asset_id > 0 ? $asset_id : 0;
438  $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
439 
440  if (!is_object($hookmanager)) {
441  require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
442  $hookmanager = new HookManager($this->db);
443  }
444 
445  $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
446  $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
447  $reshook = $hookmanager->executeHooks('updateDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
448  if (!empty($reshook)) {
449  return $reshook;
450  }
451 
452  // Check parameters
453  if (empty($asset_id) && empty($asset_model_id)) {
454  $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
455  $error++;
456  }
457  if ($error) {
458  dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
459  return -1;
460  }
461 
462  $this->db->begin();
463 
464  if ($asset_id > 0) {
465  $this->fk_asset = $asset_id;
466  $class_type = 0;
467  } else {
468  $this->fk_asset_model = $asset_model_id;
469  $class_type = 1;
470  }
471  $this->tms = dol_now();
472  $this->fk_user_modif = $user->id;
473 
474  foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
475  // Delete old accountancy codes
476  $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
477  $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
478  $resql = $this->db->query($sql);
479  if (!$resql) {
480  $this->errors[] = $langs->trans('AssetErrorDeleteDepreciationOptionsForMode', $mode_key) . ': ' . $this->db->lasterror();
481  $error++;
482  }
483 
484  if (!$error && !empty($this->deprecation_options[$mode_key])) {
485  if (!empty($mode_info['enabled_field'])) {
486  $info = explode(':', $mode_info['enabled_field']);
487  if ($this->deprecation_options[$info[0]][$info[1]] != $info[2]) {
488  continue;
489  }
490  }
491 
492  $this->setInfosForMode($mode_key, $class_type);
493 
494  $result = $this->createCommon($user, 1);
495  if ($result < 0) {
496  $this->errors = array_merge(array($langs->trans('AssetErrorInsertDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
497  $error++;
498  }
499  }
500  }
501 
502  if (!$error && $this->fk_asset > 0) {
503  // Calculation of depreciation lines (reversal and future)
504  require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
505  $asset = new Asset($this->db);
506  $result = $asset->fetch($this->fk_asset);
507  if ($result > 0) $result = $asset->calculationDepreciation();
508  if ($result < 0) {
509  $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
510  $this->errors[] = $asset->errorsToString();
511  $error++;
512  }
513  }
514 
515  if (!$error && !$notrigger) {
516  // Call trigger
517  $result = $this->call_trigger('ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
518  if ($result < 0) {
519  $error++;
520  }
521  // End call triggers
522  }
523 
524  if ($error) {
525  $this->db->rollback();
526  return -1;
527  } else {
528  $this->db->commit();
529  return 1;
530  }
531  }
532 
539  public function getRate($mode)
540  {
541  $duration = $this->deprecation_options[$mode]["duration"] > 0 ? $this->deprecation_options[$mode]["duration"] : 0;
542  $duration_type = $this->deprecation_options[$mode]["duration_type"] > 0 ? $this->deprecation_options[$mode]["duration_type"] : 0;
543 
544  return price(price2num($duration > 0 ? (100 * ($duration_type == 1 ? 12 : 1) / $duration) : 0, 2));
545  }
546 }
Class for AssetDepreciationOptions.
updateDeprecationOptions($user, $asset_id=0, $asset_model_id=0, $notrigger=0)
Update deprecation options of a asset or a asset model.
getGeneralDepreciationInfoForMode($mode)
get general depreciation info for a mode (used in depreciation card)
setDeprecationOptionsFromPost($class_type=0)
Fill deprecation_options property of object (using for data sent by forms)
setInfosForMode($mode, $class_type=0, $all_field=false)
Set object infos for a mode.
fetchDeprecationOptions($asset_id=0, $asset_model_id=0)
Load deprecation options of a asset or a asset model.
Class for Asset.
Definition: asset.class.php:31
Parent class of all other business classes (invoices, contracts, proposals, orders,...
validateField($val, $fieldKey, $fieldValue)
Return validation test result for a field.
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
createCommon(User $user, $notrigger=false)
Create object into database.
errorsToString()
Method to output saved errors.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage hooks.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
dol_eval($s, $returnvalue=0, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41