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