dolibarr 23.0.3
assetdepreciationoptions.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 Open-Dsi <support@open-dsi.fr>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Jose <jose.martinez@pichinov.com>
5 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28
33{
37 public $table_element = '';
38
72 public $fields = array();
73
78 public $deprecation_options_fields = array(
79 'economic' => array(
80 'label' => 'AssetDepreciationOptionEconomic',
81 'table' => 'asset_depreciation_options_economic',
82 'fields' => array(
83 '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,),
84 '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'),
85 'duration' => array('type' => 'integer', 'label' => 'AssetDepreciationOptionDuration', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'default' => '0', 'isameasure' => 1, 'validate' => 1,),
86 '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,),
87 '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")',),
88 'accelerated_depreciation_option' => array('type' => 'boolean', 'label' => 'AssetDepreciationOptionAcceleratedDepreciation', 'enabled' => 1, 'position' => 60, 'column_break' => true, 'notnull' => 0, 'default' => '0', 'visible' => 1, 'validate' => 1,),
89 '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,),
90 '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,),
91 '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,),
92 ),
93 ),
94 'accelerated_depreciation' => array(
95 'label' => 'AssetDepreciationOptionAcceleratedDepreciation',
96 'table' => 'asset_depreciation_options_fiscal',
97 'enabled_field' => 'economic:accelerated_depreciation_option:1',
98 'fields' => array(
99 '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,),
100 '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'),
101 'duration' => array('type' => 'integer', 'label' => 'AssetDepreciationOptionDuration', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'default' => '0', 'isameasure' => 1, 'validate' => 1,),
102 '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,),
103 '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")',),
104 '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,),
105 '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,),
106 '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,),
107 ),
108 ),
109 );
113 public $fk_asset;
117 public $fk_asset_model;
121 public $fk_user_modif;
122
126 public $deprecation_options = array();
127
131 public $depreciation_type;
135 public $degressive_coefficient;
139 public $duration;
143 public $duration_type;
147 public $accelerated_depreciation_option;
148
154 public function __construct(DoliDB $db)
155 {
156 global $langs;
157 $this->db = $db;
158
159 // Translate some data of arrayofkeyval
160 if (is_object($langs)) {
161 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
162 if (!empty($mode_info['fields']) && is_array($mode_info['fields'])) {
163 foreach ($mode_info['fields'] as $field_key => $field_info) {
164 if (!empty($field_info['arrayofkeyval']) && is_array($field_info['arrayofkeyval'])) {
165 foreach ($field_info['arrayofkeyval'] as $key => $val) {
166 $this->deprecation_options_fields[$mode_key]['fields'][$field_key]['arrayofkeyval'][$key] = $langs->trans($val);
167 }
168 }
169 }
170 }
171 }
172 }
173 }
174
183 public function setInfosForMode($mode, $class_type = 0, $all_field = false)
184 {
185 // Clean parameters
186 $mode = strtolower(trim($mode));
187
188 if (!empty($this->deprecation_options_fields[$mode])) {
189 $this->table_element = $this->deprecation_options_fields[$mode]['table'];
190 $this->fields = $this->deprecation_options_fields[$mode]['fields'];
191 foreach ($this->fields as $field_key => $field_info) {
192 if ((!empty($field_info['computed']) && !$all_field) || (!empty($field_info['only_on_asset']) && !empty($class_type))) {
193 unset($this->fields[$field_key]);
194 continue;
195 }
196
197 // Unset required option (notnull) if field disabled
198 if (!empty($field_info['enabled_field'])) {
199 $info = explode(':', $field_info['enabled_field']);
200 if (!empty($this->deprecation_options[$info[0]][$info[1]]) && $this->deprecation_options[$info[0]][$info[1]] != $info[2] && isset($this->fields[$field_key]['notnull'])) {
201 unset($this->fields[$field_key]['notnull']);
202 }
203 }
204 // Set value of the field in the object (for createCommon and setDeprecationOptionsFromPost functions)
205 $this->{$field_key} = $this->deprecation_options[$mode][$field_key] ?? null;
206 }
207
208 $this->fields['rowid'] = array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => '1', 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id");
209 if (empty($class_type)) {
210 $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,);
211 } else {
212 $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,);
213 }
214 $this->fields['tms'] = array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 501, 'notnull' => 0, 'visible' => 0,);
215 $this->fields['fk_user_modif'] = array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 511, 'notnull' => -1, 'visible' => 0,);
216 }
217
218 return 1;
219 }
220
227 public function setDeprecationOptionsFromPost($class_type = 0)
228 {
229 global $langs;
230
231 $error = 0;
232
233 $deprecation_options = array();
234 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
235 $this->setInfosForMode($mode_key, $class_type);
236
237 foreach ($mode_info['fields'] as $field_key => $field_info) {
238 if (!empty($field_info['computed'])) {
239 continue;
240 }
241
242 $html_name = $mode_key . '_' . $field_key;
243 if ($field_info['type'] == 'duration') {
244 if (GETPOST($html_name . 'hour') == '' && GETPOST($html_name . 'min') == '') {
245 continue; // The field was not submitted to be saved
246 }
247 } else {
248 if (!GETPOSTISSET($html_name)) {
249 continue; // The field was not submitted to be saved
250 }
251 }
252 // Ignore special fields
253 if (in_array($field_key, array('rowid', 'entity', 'import_key'))) {
254 continue;
255 }
256 if (in_array($field_key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
257 if (!in_array(abs($field_info['visible']), array(1, 3))) {
258 continue; // Only 1 and 3 that are case to create
259 }
260 }
261
262 // Set value to insert
263 if (in_array($field_info['type'], array('text', 'html'))) {
264 $value = GETPOST($html_name, 'restricthtml');
265 } elseif ($field_info['type'] == 'date') {
266 $value = dol_mktime(0, 0, 0, GETPOSTINT($html_name . 'month'), GETPOSTINT($html_name . 'day'), GETPOSTINT($html_name . 'year'), 'gmt'); // for date without hour, we use gmt
267 } elseif ($field_info['type'] == 'datetime') {
268 $value = dol_mktime(GETPOSTINT($html_name . 'hour'), GETPOSTINT($html_name . 'min'), GETPOSTINT($html_name . 'sec'), GETPOSTINT($html_name . 'month'), GETPOSTINT($html_name . 'day'), GETPOSTINT($html_name . 'year'), 'tzuserrel');
269 } elseif ($field_info['type'] == 'duration') {
270 $value = 60 * 60 * GETPOSTINT($html_name . 'hour') + 60 * GETPOSTINT($html_name . 'min');
271 } elseif (preg_match('/^(integer|price|real|double)/', $field_info['type'])) {
272 $value = price2num(GETPOST($html_name, 'alphanohtml')); // To fix decimal separator according to lang setup
273 } elseif ($field_info['type'] == 'boolean') {
274 $value = ((GETPOST($html_name) == '1' || GETPOST($html_name) == 'on') ? 1 : 0);
275 } elseif ($field_info['type'] == 'reference') {
276 $value = GETPOST($html_name) . ',' . GETPOST($html_name . '2');
277 } else {
278 if ($field_key == 'lang') {
279 $value = GETPOST($html_name, 'aZ09') ? GETPOST($html_name, 'aZ09') : "";
280 } else {
281 $value = GETPOST($html_name, 'alphanohtml');
282 }
283 }
284 if (preg_match('/^integer:/i', $field_info['type']) && $value == '-1') {
285 $value = ''; // This is an implicit foreign key field
286 }
287 if (!empty($field_info['foreignkey']) && $value == '-1') {
288 $value = ''; // This is an explicit foreign key field
289 }
290
291 //var_dump($field_key.' '.$value.' '.$field_info['type']);
292 $field_value = $value;
293 if ($field_info['notnull'] > 0 && $field_value == '' && !is_null($field_info['default']) && $field_info['default'] == '(PROV)') {
294 $field_value = '(PROV)';
295 } elseif ((!empty($field_info['required']) || $field_info['notnull'] > 0) && $field_value == '' && !empty($field_info['default'])) {
296 $field_value = $field_info['default'];
297 }
298 if ($field_info['notnull'] > 0 && $field_value == '' && is_null($field_info['default'])) {
299 $error++;
300 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($field_info['label'])), null, 'errors');
301 }
302 $deprecation_options[$mode_key][$field_key] = $field_value;
303
304 // Validation of fields values
305 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 1 || getDolGlobalString('MAIN_ACTIVATE_VALIDATION_RESULT')) {
306 if (!$error && !empty($field_info['validate']) && is_callable(array($this, 'validateField'))) {
307 if (!$this->validateField($mode_info['fields'], $field_key, $value)) {
308 $error++;
309 }
310 }
311 }
312 }
313 }
314 // Unset not enabled modes
315 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
316 if (!empty($mode_info['enabled_field'])) {
317 $info = explode(':', $mode_info['enabled_field']);
318 if (isset($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
319 unset($deprecation_options[$mode_key]);
320 }
321 }
322 }
323 $this->deprecation_options = $deprecation_options;
324
325 if ($error) {
326 return -1;
327 } else {
328 return 1;
329 }
330 }
331
339 public function fetchDeprecationOptions($asset_id = 0, $asset_model_id = 0)
340 {
341 global $langs, $hookmanager;
342 dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
343
344 $error = 0;
345 $this->errors = array();
346 $this->deprecation_options = array();
347
348 // Clean parameters
349 $asset_id = $asset_id > 0 ? $asset_id : 0;
350 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
351
352 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
353 $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
354 $reshook = $hookmanager->executeHooks('fetchDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
355 if (!empty($reshook)) {
356 return $reshook;
357 }
358
359 // Check parameters
360 if (empty($asset_id) && empty($asset_model_id)) {
361 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
362 $error++;
363 }
364 if ($error) {
365 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
366 return -1;
367 }
368
369 $class_type = $asset_id > 0 ? 0 : 1;
370 $deprecation_options = array();
371 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
372 $this->setInfosForMode($mode_key, $class_type);
373
374 $result = $this->fetchCommon(0, '', " AND " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id));
375 if ($result < 0) {
376 $this->errors = array_merge(array($langs->trans('AssetErrorFetchDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
377 $error++;
378 } elseif ($result > 0) {
379 foreach ($this->fields as $field_key => $field_info) {
380 if (in_array($field_key, array('rowid', 'fk_asset', 'fk_asset_model', 'tms', 'fk_user_modif'))) {
381 continue;
382 }
383 $deprecation_options[$mode_key][$field_key] = $this->{$field_key};
384 }
385 }
386 }
387 // Unset not enabled modes
388 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
389 if (!empty($mode_info['enabled_field'])) {
390 $info = explode(':', $mode_info['enabled_field']);
391 if (!empty($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
392 unset($deprecation_options[$mode_key]);
393 }
394 }
395 }
396
397 if ($error) {
398 dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
399 return -1;
400 } else {
401 $this->deprecation_options = $deprecation_options;
402 return 1;
403 }
404 }
405
413 {
414 global $hookmanager;
415 dol_syslog(__METHOD__ . " mode=$mode");
416
417 $this->errors = array();
418
419 // Clean parameters
420 $mode = strtolower(trim($mode));
421
422 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
423 $parameters = array('mode' => $mode);
424 $reshook = $hookmanager->executeHooks('getGeneralDepreciationInfoForMode', $parameters, $this); // Note that $action and $object may have been modified by some hooks
425 if ($reshook < 0) {
426 return $reshook;
427 } elseif ($reshook > 0) {
428 return $hookmanager->resArray;
429 }
430
431 $duration_type_list = $this->deprecation_options_fields[$mode]['fields']['duration_type']['arrayofkeyval'];
432
433 $mode_opts = $this->deprecation_options[$mode] ?? array();
434 $duration_type_idx = $mode_opts['duration_type'] ?? null;
435 $duration_type_label = $duration_type_list[$duration_type_idx] ?? '';
436
437 return array(
438 'base_depreciation_ht' => $mode_opts['amount_base_depreciation_ht'] ?? 0,
439 'duration' => $mode_opts['duration'] ?? 0,
440 'duration_type' => $duration_type_label,
441 'rate' => $this->getRate($mode),
442 );
443 }
444
454 public function updateDeprecationOptions($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
455 {
456 global $langs, $hookmanager;
457 dol_syslog(__METHOD__ . " user_id=".$user->id.", asset_id=".$asset_id.", asset_model_id=".$asset_model_id.", notrigger=".$notrigger);
458
459 $error = 0;
460 $this->errors = array();
461
462 // Clean parameters
463 $asset_id = $asset_id > 0 ? $asset_id : 0;
464 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
465
466 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
467 $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
468 $reshook = $hookmanager->executeHooks('updateDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
469 if (!empty($reshook)) {
470 return $reshook;
471 }
472
473 // Check parameters
474 if (empty($asset_id) && empty($asset_model_id)) {
475 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
476 $error++;
477 }
478 if ($error) {
479 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
480 return -1;
481 }
482
483 $this->db->begin();
484
485 if ($asset_id > 0) {
486 $this->fk_asset = $asset_id;
487 $class_type = 0;
488 } else {
489 $this->fk_asset_model = $asset_model_id;
490 $class_type = 1;
491 }
492 $this->tms = dol_now();
493 $this->fk_user_modif = $user->id;
494
495 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
496 // Delete old accountancy codes
497 $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
498 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
499 $resql = $this->db->query($sql);
500 if (!$resql) {
501 $this->errors[] = $langs->trans('AssetErrorDeleteDepreciationOptionsForMode', $mode_key) . ': ' . $this->db->lasterror();
502 $error++;
503 }
504
505 if (!$error && !empty($this->deprecation_options[$mode_key])) {
506 if (!empty($mode_info['enabled_field'])) {
507 list($ref_mode, $ref_field, $required) = explode(':', $mode_info['enabled_field']);
508 $flag = $this->deprecation_options[$ref_mode][$ref_field] ?? null;
509 if ((string) $flag !== (string) $required) {
510 continue;
511 }
512 }
513
514 $this->setInfosForMode($mode_key, $class_type);
515
516 $result = $this->createCommon($user, 1);
517 if ($result < 0) {
518 $this->errors = array_merge(array($langs->trans('AssetErrorInsertDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
519 $error++;
520 }
521 }
522 }
523
524 if (!$error && $this->fk_asset > 0) {
525 // Calculation of depreciation lines (reversal and future)
526 require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
527 $asset = new Asset($this->db);
528 $result = $asset->fetch($this->fk_asset);
529 if ($result > 0) {
530 $result = $asset->calculationDepreciation();
531 }
532 if ($result < 0) {
533 $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
534 $this->errors[] = $asset->errorsToString();
535 $error++;
536 }
537 }
538
539 if (!$error && !$notrigger) {
540 // Call trigger
541 $result = $this->call_trigger('ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
542 if ($result < 0) {
543 $error++;
544 }
545 // End call triggers
546 }
547
548 if ($error) {
549 $this->db->rollback();
550 return -1;
551 } else {
552 $this->db->commit();
553 return 1;
554 }
555 }
556
563 public function getRate($mode)
564 {
565 $duration = (isset($this->deprecation_options[$mode]["duration"]) && $this->deprecation_options[$mode]["duration"] > 0) ? $this->deprecation_options[$mode]["duration"] : 0;
566 $duration_type = (isset($this->deprecation_options[$mode]["duration_type"]) && $this->deprecation_options[$mode]["duration_type"] > 0) ? $this->deprecation_options[$mode]["duration_type"] : 0;
567
568 return price(price2num($duration > 0 ? (100 * ($duration_type == 1 ? 12 : 1) / $duration) : 0, 2));
569 }
570}
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.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
validateField($fields, $fieldKey, $fieldValue)
Return validation test result for a field.
createCommon(User $user, $notrigger=0)
Create object in the database.
errorsToString()
Method to output saved errors.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
Class to manage Dolibarr database access.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.