dolibarr 24.0.1
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-2026 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' => 1, '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' => 1, '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' => 1, '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' => 1, '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' => 1, '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' => 1, '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
228 protected function isEnabledFieldSatisfiedFromPost($enabledfield)
229 {
230 $info = explode(':', $enabledfield);
231 if (count($info) < 3) {
232 return true;
233 }
234
235 $htmlname = $info[0] . '_' . $info[1];
236 if (!GETPOSTISSET($htmlname)) {
237 return false; // An unchecked checkbox is not submitted at all
238 }
239
240 $value = GETPOST($htmlname, 'alphanohtml');
241 if ($value === 'on') {
242 $value = '1'; // A checked checkbox may be submitted as 'on'
243 }
244
245 return ((string) $value === (string) $info[2]);
246 }
247
254 public function setDeprecationOptionsFromPost($class_type = 0)
255 {
256 global $langs;
257
258 $error = 0;
259
260 $deprecation_options = array();
261 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
262 // A mode disabled by its enabled_field must not be validated at all. The form submits the
263 // fields of the hidden block anyway, empty, so a required field of that block (the
264 // degressive coefficient) makes the whole page fail. The block is dropped further below,
265 // but only after its fields have been validated, which is too late.
266 if (!empty($mode_info['enabled_field']) && !$this->isEnabledFieldSatisfiedFromPost($mode_info['enabled_field'])) {
267 continue;
268 }
269
270 $this->setInfosForMode($mode_key, $class_type);
271
272 foreach ($mode_info['fields'] as $field_key => $field_info) {
273 if (!empty($field_info['computed'])) {
274 continue;
275 }
276 // Same thing for a single field hidden by its own enabled_field: the degressive
277 // coefficient is required but hidden as soon as the depreciation type is not degressive
278 if (!empty($field_info['enabled_field']) && !$this->isEnabledFieldSatisfiedFromPost($field_info['enabled_field'])) {
279 continue;
280 }
281
282 $html_name = $mode_key . '_' . $field_key;
283 if ($field_info['type'] == 'duration') {
284 if (GETPOST($html_name . 'hour') == '' && GETPOST($html_name . 'min') == '') {
285 continue; // The field was not submitted to be saved
286 }
287 } else {
288 if (!GETPOSTISSET($html_name)) {
289 continue; // The field was not submitted to be saved
290 }
291 }
292 // Ignore special fields
293 if (in_array($field_key, array('rowid', 'entity', 'import_key'))) {
294 continue;
295 }
296 if (in_array($field_key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
297 if (!in_array(abs($field_info['visible']), array(1, 3))) {
298 continue; // Only 1 and 3 that are case to create
299 }
300 }
301
302 // Set value to insert
303 if (in_array($field_info['type'], array('text', 'html'))) {
304 $value = GETPOST($html_name, 'restricthtml');
305 } elseif ($field_info['type'] == 'date') {
306 $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
307 } elseif ($field_info['type'] == 'datetime') {
308 $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');
309 } elseif ($field_info['type'] == 'duration') {
310 $value = 60 * 60 * GETPOSTINT($html_name . 'hour') + 60 * GETPOSTINT($html_name . 'min');
311 } elseif (preg_match('/^(integer|price|real|double)/', $field_info['type'])) {
312 $value = price2num(GETPOST($html_name, 'alphanohtml')); // To fix decimal separator according to lang setup
313 } elseif ($field_info['type'] == 'boolean') {
314 $value = ((GETPOST($html_name) == '1' || GETPOST($html_name) == 'on') ? 1 : 0);
315 } elseif ($field_info['type'] == 'reference') {
316 $value = GETPOST($html_name) . ',' . GETPOST($html_name . '2');
317 } else {
318 if ($field_key == 'lang') {
319 $value = GETPOST($html_name, 'aZ09') ? GETPOST($html_name, 'aZ09') : "";
320 } else {
321 $value = GETPOST($html_name, 'alphanohtml');
322 }
323 }
324 if (preg_match('/^integer:/i', $field_info['type']) && $value == '-1') {
325 $value = ''; // This is an implicit foreign key field
326 }
327 if (!empty($field_info['foreignkey']) && $value == '-1') {
328 $value = ''; // This is an explicit foreign key field
329 }
330
331 //var_dump($field_key.' '.$value.' '.$field_info['type']);
332 $field_value = $value;
333 if ($field_info['notnull'] > 0 && $field_value == '' && !is_null($field_info['default']) && $field_info['default'] == '(PROV)') {
334 $field_value = '(PROV)';
335 } elseif ((!empty($field_info['required']) || $field_info['notnull'] > 0) && $field_value == '' && !empty($field_info['default'])) {
336 $field_value = $field_info['default'];
337 }
338 if ($field_info['notnull'] > 0 && $field_value == '' && is_null($field_info['default'])) {
339 $error++;
340 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($field_info['label'])), null, 'errors');
341 }
342 $deprecation_options[$mode_key][$field_key] = $field_value;
343
344 // Validation of fields values
345 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 1 || getDolGlobalString('MAIN_ACTIVATE_VALIDATION_RESULT')) {
346 if (!$error && !empty($field_info['validate']) && is_callable(array($this, 'validateField'))) {
347 if (!$this->validateField($mode_info['fields'], $field_key, $value)) {
348 $error++;
349 }
350 }
351 }
352 }
353 }
354 // Unset not enabled modes
355 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
356 if (!empty($mode_info['enabled_field'])) {
357 $info = explode(':', $mode_info['enabled_field']);
358 if (isset($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
359 unset($deprecation_options[$mode_key]);
360 }
361 }
362 }
363 $this->deprecation_options = $deprecation_options;
364
365 if ($error) {
366 return -1;
367 } else {
368 return 1;
369 }
370 }
371
379 public function fetchDeprecationOptions($asset_id = 0, $asset_model_id = 0)
380 {
381 global $langs, $hookmanager;
382 dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
383
384 $error = 0;
385 $this->errors = array();
386 $this->deprecation_options = array();
387
388 // Clean parameters
389 $asset_id = $asset_id > 0 ? $asset_id : 0;
390 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
391
392 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
393 $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
394 $reshook = $hookmanager->executeHooks('fetchDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
395 if (!empty($reshook)) {
396 return $reshook;
397 }
398
399 // Check parameters
400 if (empty($asset_id) && empty($asset_model_id)) {
401 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
402 $error++;
403 }
404 if ($error) {
405 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
406 return -1;
407 }
408
409 $class_type = $asset_id > 0 ? 0 : 1;
410 $deprecation_options = array();
411 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
412 $this->setInfosForMode($mode_key, $class_type);
413
414 $result = $this->fetchCommon(0, '', " AND " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id));
415 if ($result < 0) {
416 $this->errors = array_merge(array($langs->trans('AssetErrorFetchDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
417 $error++;
418 } elseif ($result > 0) {
419 foreach ($this->fields as $field_key => $field_info) {
420 if (in_array($field_key, array('rowid', 'fk_asset', 'fk_asset_model', 'tms', 'fk_user_modif'))) {
421 continue;
422 }
423 $deprecation_options[$mode_key][$field_key] = $this->{$field_key};
424 }
425 }
426 }
427 // Unset not enabled modes
428 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
429 if (!empty($mode_info['enabled_field'])) {
430 $info = explode(':', $mode_info['enabled_field']);
431 if (!empty($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
432 unset($deprecation_options[$mode_key]);
433 }
434 }
435 }
436
437 if ($error) {
438 dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
439 return -1;
440 } else {
441 $this->deprecation_options = $deprecation_options;
442 return 1;
443 }
444 }
445
453 {
454 global $hookmanager;
455 dol_syslog(__METHOD__ . " mode=$mode");
456
457 $this->errors = array();
458
459 // Clean parameters
460 $mode = strtolower(trim($mode));
461
462 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
463 $parameters = array('mode' => $mode);
464 $reshook = $hookmanager->executeHooks('getGeneralDepreciationInfoForMode', $parameters, $this); // Note that $action and $object may have been modified by some hooks
465 if ($reshook < 0) {
466 return $reshook;
467 } elseif ($reshook > 0) {
468 return $hookmanager->resArray;
469 }
470
471 $duration_type_list = $this->deprecation_options_fields[$mode]['fields']['duration_type']['arrayofkeyval'];
472
473 $mode_opts = $this->deprecation_options[$mode] ?? array();
474 $duration_type_idx = $mode_opts['duration_type'] ?? null;
475 $duration_type_label = $duration_type_list[$duration_type_idx] ?? '';
476
477 return array(
478 'base_depreciation_ht' => $mode_opts['amount_base_depreciation_ht'] ?? 0,
479 'duration' => $mode_opts['duration'] ?? 0,
480 'duration_type' => $duration_type_label,
481 'rate' => $this->getRate($mode),
482 );
483 }
484
494 public function updateDeprecationOptions($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
495 {
496 global $langs, $hookmanager;
497 dol_syslog(__METHOD__ . " user_id=".$user->id.", asset_id=".$asset_id.", asset_model_id=".$asset_model_id.", notrigger=".$notrigger);
498
499 $error = 0;
500 $this->errors = array();
501
502 // Clean parameters
503 $asset_id = $asset_id > 0 ? $asset_id : 0;
504 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
505
506 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
507 $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
508 $reshook = $hookmanager->executeHooks('updateDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
509 if (!empty($reshook)) {
510 return $reshook;
511 }
512
513 // Check parameters
514 if (empty($asset_id) && empty($asset_model_id)) {
515 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
516 $error++;
517 }
518 if ($error) {
519 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
520 return -1;
521 }
522
523 $this->db->begin();
524
525 if ($asset_id > 0) {
526 $this->fk_asset = $asset_id;
527 $class_type = 0;
528 } else {
529 $this->fk_asset_model = $asset_model_id;
530 $class_type = 1;
531 }
532 $this->tms = dol_now();
533 $this->fk_user_modif = $user->id;
534
535 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
536 // Delete old accountancy codes
537 $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table']; // From safe table @phan-suppress-current-line SqlInjection
538 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
539 $resql = $this->db->query($sql);
540 if (!$resql) {
541 $this->errors[] = $langs->trans('AssetErrorDeleteDepreciationOptionsForMode', $mode_key) . ': ' . $this->db->lasterror();
542 $error++;
543 }
544
545 if (!$error && !empty($this->deprecation_options[$mode_key])) {
546 if (!empty($mode_info['enabled_field'])) {
547 list($ref_mode, $ref_field, $required) = explode(':', $mode_info['enabled_field']);
548 $flag = $this->deprecation_options[$ref_mode][$ref_field] ?? null;
549 if ((string) $flag !== (string) $required) {
550 continue;
551 }
552 }
553
554 $this->setInfosForMode($mode_key, $class_type);
555
556 $result = $this->createCommon($user, 1);
557 if ($result < 0) {
558 $this->errors = array_merge(array($langs->trans('AssetErrorInsertDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
559 $error++;
560 }
561 }
562 }
563
564 if (!$error && $this->fk_asset > 0) {
565 // Calculation of depreciation lines (reversal and future)
566 require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
567 $asset = new Asset($this->db);
568 $result = $asset->fetch($this->fk_asset);
569 if ($result > 0) {
570 $result = $asset->calculationDepreciation();
571 }
572 if ($result < 0) {
573 $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
574 $this->errors[] = $asset->errorsToString();
575 $error++;
576 }
577 }
578
579 if (!$error && !$notrigger) {
580 // Call trigger
581 $result = $this->call_trigger('ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
582 if ($result < 0) {
583 $error++;
584 }
585 // End call triggers
586 }
587
588 if ($error) {
589 $this->db->rollback();
590 return -1;
591 } else {
592 $this->db->commit();
593 return 1;
594 }
595 }
596
603 public function getRate($mode)
604 {
605 $duration = (isset($this->deprecation_options[$mode]["duration"]) && $this->deprecation_options[$mode]["duration"] > 0) ? $this->deprecation_options[$mode]["duration"] : 0;
606 $duration_type = (isset($this->deprecation_options[$mode]["duration_type"]) && $this->deprecation_options[$mode]["duration_type"] > 0) ? $this->deprecation_options[$mode]["duration_type"] : 0;
607
608 return price(price2num($duration > 0 ? (100 * ($duration_type == 1 ? 12 : 1) / $duration) : 0, 2));
609 }
610}
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)
isEnabledFieldSatisfiedFromPost($enabledfield)
Return whether an 'enabled_field' condition ("mode_key:field_key:value") is satisfied by the data of ...
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
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, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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.