27require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
37 public $table_element =
'';
72 public $fields = array();
78 public $deprecation_options_fields = array(
80 'label' =>
'AssetDepreciationOptionEconomic',
81 'table' =>
'asset_depreciation_options_economic',
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'),
'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,),
94 'accelerated_depreciation' => array(
95 'label' =>
'AssetDepreciationOptionAcceleratedDepreciation',
96 'table' =>
'asset_depreciation_options_fiscal',
97 'enabled_field' =>
'economic:accelerated_depreciation_option:1',
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'),
'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,),
117 public $fk_asset_model;
121 public $fk_user_modif;
126 public $deprecation_options = array();
131 public $depreciation_type;
135 public $degressive_coefficient;
143 public $duration_type;
147 public $accelerated_depreciation_option;
155 public $amount_base_depreciation_ht;
159 public $amount_base_deductible_ht;
163 public $total_amount_last_depreciation_ht;
176 if (is_object($langs)) {
177 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
178 if (!empty($mode_info[
'fields']) && is_array($mode_info[
'fields'])) {
179 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
180 if (!empty($field_info[
'arrayofkeyval']) && is_array($field_info[
'arrayofkeyval'])) {
181 foreach ($field_info[
'arrayofkeyval'] as $key => $val) {
182 $this->deprecation_options_fields[$mode_key][
'fields'][$field_key][
'arrayofkeyval'][$key] = $langs->trans($val);
202 $mode = strtolower(trim($mode));
204 if (!empty($this->deprecation_options_fields[$mode])) {
205 $this->table_element = $this->deprecation_options_fields[$mode][
'table'];
206 $this->fields = $this->deprecation_options_fields[$mode][
'fields'];
207 foreach ($this->fields as $field_key => $field_info) {
208 if ((!empty($field_info[
'computed']) && !$all_field) || (!empty($field_info[
'only_on_asset']) && !empty($class_type))) {
209 unset($this->fields[$field_key]);
214 if (!empty($field_info[
'enabled_field'])) {
215 $info = explode(
':', $field_info[
'enabled_field']);
217 if (isset($this->deprecation_options[$info[0]][$info[1]]) && (
string) $this->deprecation_options[$info[0]][$info[1]] !== (
string) $info[2] && isset($this->fields[$field_key][
'notnull'])) {
218 unset($this->fields[$field_key][
'notnull']);
222 $this->{$field_key} = $this->deprecation_options[$mode][$field_key] ??
null;
225 $this->fields[
'rowid'] = array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' =>
'1',
'position' => 1,
'notnull' => 1,
'visible' => 0,
'noteditable' => 1,
'index' => 1,
'css' =>
'left',
'comment' =>
"Id");
226 if (empty($class_type)) {
227 $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,);
229 $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,);
231 $this->fields[
'tms'] = array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'position' => 501,
'notnull' => 0,
'visible' => 0,);
232 $this->fields[
'fk_user_modif'] = array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'position' => 511,
'notnull' => -1,
'visible' => 0,);
247 $info = explode(
':', $enabledfield);
248 if (count($info) < 3) {
252 $htmlname = $info[0] .
'_' . $info[1];
257 $value =
GETPOST($htmlname,
'alphanohtml');
258 if ($value ===
'on') {
262 return ((
string) $value === (
string) $info[2]);
277 $deprecation_options = array();
278 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
289 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
290 if (!empty($field_info[
'computed'])) {
299 $html_name = $mode_key .
'_' . $field_key;
300 if ($field_info[
'type'] ==
'duration') {
301 if (
GETPOST($html_name .
'hour') ==
'' &&
GETPOST($html_name .
'min') ==
'') {
310 if (in_array($field_key, array(
'rowid',
'entity',
'import_key'))) {
313 if (in_array($field_key, array(
'date_creation',
'tms',
'fk_user_creat',
'fk_user_modif'))) {
314 if (!in_array(abs($field_info[
'visible']), array(1, 3))) {
320 if (in_array($field_info[
'type'], array(
'text',
'html'))) {
321 $value =
GETPOST($html_name,
'restricthtml');
322 } elseif ($field_info[
'type'] ==
'date') {
324 } elseif ($field_info[
'type'] ==
'datetime') {
326 } elseif ($field_info[
'type'] ==
'duration') {
328 } elseif (preg_match(
'/^(integer|price|real|double)/', $field_info[
'type'])) {
330 } elseif ($field_info[
'type'] ==
'boolean') {
331 $value = ((
GETPOST($html_name) ==
'1' ||
GETPOST($html_name) ==
'on') ? 1 : 0);
332 } elseif ($field_info[
'type'] ==
'reference') {
335 if ($field_key ==
'lang') {
336 $value =
GETPOST($html_name,
'aZ09') ?
GETPOST($html_name,
'aZ09') :
"";
338 $value =
GETPOST($html_name,
'alphanohtml');
341 if (preg_match(
'/^integer:/i', $field_info[
'type']) && $value ==
'-1') {
344 if (!empty($field_info[
'foreignkey']) && $value ==
'-1') {
349 $field_value = $value;
351 if ($field_info[
'notnull'] > 0 && $field_value ==
'' && !is_null($field_info[
'default']) && $field_info[
'default'] ==
'(PROV)') {
352 $field_value =
'(PROV)';
353 } elseif ((!empty($field_info[
'required']) || $field_info[
'notnull'] > 0) && $field_value ==
'' && !empty($field_info[
'default'])) {
354 $field_value = $field_info[
'default'];
356 if ($field_info[
'notnull'] > 0 && $field_value ==
'' && is_null($field_info[
'default'])) {
358 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($field_info[
'label'])),
null,
'errors');
360 $deprecation_options[$mode_key][$field_key] = $field_value;
364 if (!$error && !empty($field_info[
'validate']) && is_callable(array($this,
'validateField'))) {
365 if (!$this->
validateField($mode_info[
'fields'], $field_key, $value)) {
373 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
374 if (!empty($mode_info[
'enabled_field'])) {
375 $info = explode(
':', $mode_info[
'enabled_field']);
376 if (isset($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
377 unset($deprecation_options[$mode_key]);
381 $this->deprecation_options = $deprecation_options;
399 global $langs, $hookmanager;
400 dol_syslog(__METHOD__ .
" asset_id=$asset_id, asset_model_id=$asset_model_id");
403 $this->errors = array();
404 $this->deprecation_options = array();
407 $asset_id = $asset_id > 0 ? $asset_id : 0;
408 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
410 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
411 $parameters = array(
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
412 $reshook = $hookmanager->executeHooks(
'fetchDepreciationOptions', $parameters, $this);
413 if (!empty($reshook)) {
418 if (empty($asset_id) && empty($asset_model_id)) {
419 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
427 $class_type = $asset_id > 0 ? 0 : 1;
428 $deprecation_options = array();
429 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
432 $result = $this->
fetchCommon(0,
'',
" AND " . ($asset_id > 0 ?
" fk_asset = " . (
int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id));
434 $this->errors = array_merge(array($langs->trans(
'AssetErrorFetchDepreciationOptionsForMode', $mode_key) .
':'), $this->errors);
436 } elseif ($result > 0) {
437 foreach ($this->fields as $field_key => $field_info) {
438 if (in_array($field_key, array(
'rowid',
'fk_asset',
'fk_asset_model',
'tms',
'fk_user_modif'))) {
441 $deprecation_options[$mode_key][$field_key] = $this->{$field_key};
446 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
447 if (!empty($mode_info[
'enabled_field'])) {
448 $info = explode(
':', $mode_info[
'enabled_field']);
450 if (isset($deprecation_options[$info[0]][$info[1]]) && (
string) $deprecation_options[$info[0]][$info[1]] !== (
string) $info[2]) {
451 unset($deprecation_options[$mode_key]);
460 $this->deprecation_options = $deprecation_options;
476 $this->errors = array();
479 $mode = strtolower(trim($mode));
481 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
482 $parameters = array(
'mode' => $mode);
483 $reshook = $hookmanager->executeHooks(
'getGeneralDepreciationInfoForMode', $parameters, $this);
486 } elseif ($reshook > 0) {
487 return $hookmanager->resArray;
490 $duration_type_list = $this->deprecation_options_fields[$mode][
'fields'][
'duration_type'][
'arrayofkeyval'];
492 $mode_opts = $this->deprecation_options[$mode] ?? array();
493 $duration_type_idx = $mode_opts[
'duration_type'] ??
null;
494 $duration_type_label = $duration_type_list[$duration_type_idx] ??
'';
497 'base_depreciation_ht' => $mode_opts[
'amount_base_depreciation_ht'] ?? 0,
498 'duration' => $mode_opts[
'duration'] ?? 0,
499 'duration_type' => $duration_type_label,
500 'rate' => $this->
getRate($mode),
515 global $langs, $hookmanager;
516 dol_syslog(__METHOD__ .
" user_id=".$user->id.
", asset_id=".$asset_id.
", asset_model_id=".$asset_model_id.
", notrigger=".$notrigger);
519 $this->errors = array();
522 $asset_id = $asset_id > 0 ? $asset_id : 0;
523 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
525 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
526 $parameters = array(
'user' => $user,
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
527 $reshook = $hookmanager->executeHooks(
'updateDepreciationOptions', $parameters, $this);
528 if (!empty($reshook)) {
533 if (empty($asset_id) && empty($asset_model_id)) {
534 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
545 $this->fk_asset = $asset_id;
548 $this->fk_asset_model = $asset_model_id;
552 $this->fk_user_modif = $user->id;
554 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
556 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
557 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
558 $resql = $this->db->query($sql);
560 $this->errors[] = $langs->trans(
'AssetErrorDeleteDepreciationOptionsForMode', $mode_key) .
': ' . $this->db->lasterror();
564 if (!$error && !empty($this->deprecation_options[$mode_key])) {
565 if (!empty($mode_info[
'enabled_field'])) {
566 list($ref_mode, $ref_field, $required) = explode(
':', $mode_info[
'enabled_field']);
567 $flag = $this->deprecation_options[$ref_mode][$ref_field] ??
null;
568 if ((
string) $flag !== (
string) $required) {
577 $this->errors = array_merge(array($langs->trans(
'AssetErrorInsertDepreciationOptionsForMode', $mode_key) .
':'), $this->errors);
583 if (!$error && $this->fk_asset > 0) {
585 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
586 $asset =
new Asset($this->db);
587 $result = $asset->fetch($this->fk_asset);
589 $result = $asset->calculationDepreciation();
592 $this->errors[] = $langs->trans(
'AssetErrorCalculationDepreciationLines');
593 $this->errors[] = $asset->errorsToString();
598 if (!$error && !$notrigger) {
600 $result = $this->call_trigger(
'ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
608 $this->db->rollback();
624 $duration = (isset($this->deprecation_options[$mode][
"duration"]) && $this->deprecation_options[$mode][
"duration"] > 0) ? $this->deprecation_options[$mode][
"duration"] : 0;
625 $duration_type = (isset($this->deprecation_options[$mode][
"duration_type"]) && $this->deprecation_options[$mode][
"duration_type"] > 0) ? $this->deprecation_options[$mode][
"duration_type"] : 0;
627 return price(
price2num($duration > 0 ? (100 * ($duration_type == 1 ? 12 : 1) / $duration) : 0, 2));
Class for AssetDepreciationOptions.
__construct(DoliDB $db)
Constructor.
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.
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...
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.