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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
31{
35 public $table_element = '';
36
70 public $fields = array();
71
76 public $deprecation_options_fields = array(
77 'economic' => array(
78 'label' => 'AssetDepreciationOptionEconomic',
79 'table' => 'asset_depreciation_options_economic',
80 'fields' => array(
81 '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,),
82 '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'),
83 'duration' => array('type' => 'integer', 'label' => 'AssetDepreciationOptionDuration', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'default' => '0', 'isameasure' => 1, 'validate' => 1,),
84 '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,),
85 '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")',),
86 'accelerated_depreciation_option' => array('type' => 'boolean', 'label' => 'AssetDepreciationOptionAcceleratedDepreciation', 'enabled' => 1, 'position' => 60, 'column_break' => true, 'notnull' => 0, 'default' => '0', 'visible' => 1, 'validate' => 1,),
87 '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,),
88 '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,),
89 '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,),
90 ),
91 ),
92 'accelerated_depreciation' => array(
93 'label' => 'AssetDepreciationOptionAcceleratedDepreciation',
94 'table' => 'asset_depreciation_options_fiscal',
95 'enabled_field' => 'economic:accelerated_depreciation_option:1',
96 'fields' => array(
97 '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,),
98 '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'),
99 'duration' => array('type' => 'integer', 'label' => 'AssetDepreciationOptionDuration', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'default' => '0', 'isameasure' => 1, 'validate' => 1,),
100 '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,),
101 '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")',),
102 '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,),
103 '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,),
104 '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,),
105 ),
106 ),
107 );
111 public $fk_asset;
115 public $fk_asset_model;
119 public $fk_user_modif;
120
124 public $deprecation_options = array();
125
129 public $depreciation_type;
133 public $degressive_coefficient;
137 public $duration;
141 public $duration_type;
145 public $accelerated_depreciation_option;
146
152 public function __construct(DoliDB $db)
153 {
154 global $langs;
155 $this->db = $db;
156
157 // Translate some data of arrayofkeyval
158 if (is_object($langs)) {
159 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
160 if (!empty($mode_info['fields']) && is_array($mode_info['fields'])) {
161 foreach ($mode_info['fields'] as $field_key => $field_info) {
162 if (!empty($field_info['arrayofkeyval']) && is_array($field_info['arrayofkeyval'])) {
163 foreach ($field_info['arrayofkeyval'] as $key => $val) {
164 $this->deprecation_options_fields[$mode_key]['fields'][$field_key]['arrayofkeyval'][$key] = $langs->trans($val);
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172
181 public function setInfosForMode($mode, $class_type = 0, $all_field = false)
182 {
183 // Clean parameters
184 $mode = strtolower(trim($mode));
185
186 if (!empty($this->deprecation_options_fields[$mode])) {
187 $this->table_element = $this->deprecation_options_fields[$mode]['table'];
188 $this->fields = $this->deprecation_options_fields[$mode]['fields'];
189 foreach ($this->fields as $field_key => $field_info) {
190 if ((!empty($field_info['computed']) && !$all_field) || (!empty($field_info['only_on_asset']) && !empty($class_type))) {
191 unset($this->fields[$field_key]);
192 continue;
193 }
194
195 // Unset required option (notnull) if field disabled
196 if (!empty($field_info['enabled_field'])) {
197 $info = explode(':', $field_info['enabled_field']);
198 if (!empty($this->deprecation_options[$info[0]][$info[1]]) && $this->deprecation_options[$info[0]][$info[1]] != $info[2] && isset($this->fields[$field_key]['notnull'])) {
199 unset($this->fields[$field_key]['notnull']);
200 }
201 }
202 // Set value of the field in the object (for createCommon and setDeprecationOptionsFromPost functions)
203 $this->{$field_key} = $this->deprecation_options[$mode][$field_key] ?? null;
204 }
205
206 $this->fields['rowid'] = array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id");
207 if (empty($class_type)) {
208 $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,);
209 } else {
210 $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,);
211 }
212 $this->fields['tms'] = array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 501, 'notnull' => 0, 'visible' => 0,);
213 $this->fields['fk_user_modif'] = array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 511, 'notnull' => -1, 'visible' => 0,);
214 }
215
216 return 1;
217 }
218
225 public function setDeprecationOptionsFromPost($class_type = 0)
226 {
227 global $conf, $langs;
228
229 $error = 0;
230
231 $deprecation_options = array();
232 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
233 $this->setInfosForMode($mode_key, $class_type);
234
235 foreach ($mode_info['fields'] as $field_key => $field_info) {
236 if (!empty($field_info['computed'])) {
237 continue;
238 }
239
240 $html_name = $mode_key . '_' . $field_key;
241 if ($field_info['type'] == 'duration') {
242 if (GETPOST($html_name . 'hour') == '' && GETPOST($html_name . 'min') == '') {
243 continue; // The field was not submitted to be saved
244 }
245 } else {
246 if (!GETPOSTISSET($html_name)) {
247 continue; // The field was not submitted to be saved
248 }
249 }
250 // Ignore special fields
251 if (in_array($field_key, array('rowid', 'entity', 'import_key'))) {
252 continue;
253 }
254 if (in_array($field_key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
255 if (!in_array(abs($field_info['visible']), array(1, 3))) {
256 continue; // Only 1 and 3 that are case to create
257 }
258 }
259
260 // Set value to insert
261 if (in_array($field_info['type'], array('text', 'html'))) {
262 $value = GETPOST($html_name, 'restricthtml');
263 } elseif ($field_info['type'] == 'date') {
264 $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
265 } elseif ($field_info['type'] == 'datetime') {
266 $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');
267 } elseif ($field_info['type'] == 'duration') {
268 $value = 60 * 60 * GETPOSTINT($html_name . 'hour') + 60 * GETPOSTINT($html_name . 'min');
269 } elseif (preg_match('/^(integer|price|real|double)/', $field_info['type'])) {
270 $value = price2num(GETPOST($html_name, 'alphanohtml')); // To fix decimal separator according to lang setup
271 } elseif ($field_info['type'] == 'boolean') {
272 $value = ((GETPOST($html_name) == '1' || GETPOST($html_name) == 'on') ? 1 : 0);
273 } elseif ($field_info['type'] == 'reference') {
274 // todo to check
275 $tmparraykey = array(); //array_keys($object->param_list);
276 $value = $tmparraykey[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_FEATURE_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 ($deprecation_options[$info[0]][$info[1]] != $info[2]) {
319 unset($deprecation_options[$info[0]][$info[1]]);
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 (isset($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
392 unset($deprecation_options[$info[0]][$info[1]]);
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 return array(
434 'base_depreciation_ht' => $this->deprecation_options[$mode]['amount_base_depreciation_ht'],
435 'duration' => $this->deprecation_options[$mode]['duration'],
436 'duration_type' => $duration_type_list[$this->deprecation_options[$mode]['duration_type']],
437 'rate' => $this->getRate($mode),
438 );
439 }
440
450 public function updateDeprecationOptions($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
451 {
452 global $langs, $hookmanager;
453 dol_syslog(__METHOD__ . " user_id=".$user->id.", asset_id=".$asset_id.", asset_model_id=".$asset_model_id.", notrigger=".$notrigger);
454
455 $error = 0;
456 $this->errors = array();
457
458 // Clean parameters
459 $asset_id = $asset_id > 0 ? $asset_id : 0;
460 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
461
462 $hookmanager->initHooks(array('assetdepreciationoptionsdao'));
463 $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
464 $reshook = $hookmanager->executeHooks('updateDepreciationOptions', $parameters, $this); // Note that $action and $object may have been modified by some hooks
465 if (!empty($reshook)) {
466 return $reshook;
467 }
468
469 // Check parameters
470 if (empty($asset_id) && empty($asset_model_id)) {
471 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
472 $error++;
473 }
474 if ($error) {
475 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
476 return -1;
477 }
478
479 $this->db->begin();
480
481 if ($asset_id > 0) {
482 $this->fk_asset = $asset_id;
483 $class_type = 0;
484 } else {
485 $this->fk_asset_model = $asset_model_id;
486 $class_type = 1;
487 }
488 $this->tms = dol_now();
489 $this->fk_user_modif = $user->id;
490
491 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
492 // Delete old accountancy codes
493 $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
494 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
495 $resql = $this->db->query($sql);
496 if (!$resql) {
497 $this->errors[] = $langs->trans('AssetErrorDeleteDepreciationOptionsForMode', $mode_key) . ': ' . $this->db->lasterror();
498 $error++;
499 }
500
501 if (!$error && !empty($this->deprecation_options[$mode_key])) {
502 if (!empty($mode_info['enabled_field'])) {
503 $info = explode(':', $mode_info['enabled_field']);
504 if ($this->deprecation_options[$info[0]][$info[1]] != $info[2]) {
505 continue;
506 }
507 }
508
509 $this->setInfosForMode($mode_key, $class_type);
510
511 $result = $this->createCommon($user, 1);
512 if ($result < 0) {
513 $this->errors = array_merge(array($langs->trans('AssetErrorInsertDepreciationOptionsForMode', $mode_key) . ':'), $this->errors);
514 $error++;
515 }
516 }
517 }
518
519 if (!$error && $this->fk_asset > 0) {
520 // Calculation of depreciation lines (reversal and future)
521 require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
522 $asset = new Asset($this->db);
523 $result = $asset->fetch($this->fk_asset);
524 if ($result > 0) {
525 $result = $asset->calculationDepreciation();
526 }
527 if ($result < 0) {
528 $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
529 $this->errors[] = $asset->errorsToString();
530 $error++;
531 }
532 }
533
534 if (!$error && !$notrigger) {
535 // Call trigger
536 $result = $this->call_trigger('ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
537 if ($result < 0) {
538 $error++;
539 }
540 // End call triggers
541 }
542
543 if ($error) {
544 $this->db->rollback();
545 return -1;
546 } else {
547 $this->db->commit();
548 return 1;
549 }
550 }
551
558 public function getRate($mode)
559 {
560 $duration = (isset($this->deprecation_options[$mode]["duration"]) && $this->deprecation_options[$mode]["duration"] > 0) ? $this->deprecation_options[$mode]["duration"] : 0;
561 $duration_type = (isset($this->deprecation_options[$mode]["duration_type"]) && $this->deprecation_options[$mode]["duration_type"] > 0) ? $this->deprecation_options[$mode]["duration_type"] : 0;
562
563 return price(price2num($duration > 0 ? (100 * ($duration_type == 1 ? 12 : 1) / $duration) : 0, 2));
564 }
565}
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.