25require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
35 public $table_element =
'';
70 public $fields = array();
76 public $deprecation_options_fields = array(
78 'label' =>
'AssetDepreciationOptionEconomic',
79 'table' =>
'asset_depreciation_options_economic',
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'),
'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',),
92 'accelerated_depreciation' => array(
93 'label' =>
'AssetDepreciationOptionAcceleratedDepreciation',
94 'table' =>
'asset_depreciation_options_fiscal',
95 'enabled_field' =>
'economic:accelerated_depreciation_option:1',
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'),
'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',),
109 public $fk_asset_model;
110 public $fk_user_modif;
115 public $deprecation_options = array();
117 public $depreciation_type;
118 public $degressive_coefficient;
120 public $duration_type;
121 public $accelerated_depreciation_option;
134 if (is_object($langs)) {
135 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
136 if (!empty($mode_info[
'fields']) && is_array($mode_info[
'fields'])) {
137 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
138 if (!empty($field_info[
'arrayofkeyval']) && is_array($field_info[
'arrayofkeyval'])) {
139 foreach ($field_info[
'arrayofkeyval'] as $key => $val) {
140 $this->deprecation_options_fields[$mode_key][
'fields'][$field_key][
'arrayofkeyval'][$key] = $langs->trans($val);
160 $mode = strtolower(trim($mode));
162 if (!empty($this->deprecation_options_fields[$mode])) {
163 $this->table_element = $this->deprecation_options_fields[$mode][
'table'];
164 $this->fields = $this->deprecation_options_fields[$mode][
'fields'];
165 foreach ($this->fields as $field_key => $field_info) {
166 if ((!empty($field_info[
'computed']) && !$all_field) || (!empty($field_info[
'only_on_asset']) && !empty($class_type))) {
167 unset($this->fields[$field_key]);
172 if (!empty($field_info[
'enabled_field'])) {
173 $info = explode(
':', $field_info[
'enabled_field']);
174 if (!empty($this->deprecation_options[$info[0]][$info[1]]) && $this->deprecation_options[$info[0]][$info[1]] != $info[2] && isset($this->fields[$field_key][
'notnull'])) {
175 unset($this->fields[$field_key][
'notnull']);
179 $this->{$field_key} = $this->deprecation_options[$mode][$field_key] ??
null;
182 $this->fields[
'rowid'] = array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' =>
'1',
'position' => 1,
'notnull' => 1,
'visible' => 0,
'noteditable' =>
'1',
'index' => 1,
'css' =>
'left',
'comment' =>
"Id");
183 if (empty($class_type)) {
184 $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',);
186 $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',);
188 $this->fields[
'tms'] = array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' =>
'1',
'position' => 501,
'notnull' => 0,
'visible' => 0,);
189 $this->fields[
'fk_user_modif'] = array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' =>
'1',
'position' => 511,
'notnull' => -1,
'visible' => 0,);
203 global $conf, $langs;
207 $deprecation_options = array();
208 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
211 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
212 if (!empty($field_info[
'computed'])) {
216 $html_name = $mode_key .
'_' . $field_key;
217 if ($field_info[
'type'] ==
'duration') {
218 if (
GETPOST($html_name .
'hour') ==
'' &&
GETPOST($html_name .
'min') ==
'') {
222 if (!GETPOSTISSET($html_name)) {
227 if (in_array($field_key, array(
'rowid',
'entity',
'import_key'))) {
230 if (in_array($field_key, array(
'date_creation',
'tms',
'fk_user_creat',
'fk_user_modif'))) {
231 if (!in_array(abs($field_info[
'visible']), array(1, 3))) {
237 if (in_array($field_info[
'type'], array(
'text',
'html'))) {
238 $value =
GETPOST($html_name,
'restricthtml');
239 } elseif ($field_info[
'type'] ==
'date') {
241 } elseif ($field_info[
'type'] ==
'datetime') {
243 } elseif ($field_info[
'type'] ==
'duration') {
245 } elseif (preg_match(
'/^(integer|price|real|double)/', $field_info[
'type'])) {
247 } elseif ($field_info[
'type'] ==
'boolean') {
248 $value = ((
GETPOST($html_name) ==
'1' ||
GETPOST($html_name) ==
'on') ? 1 : 0);
249 } elseif ($field_info[
'type'] ==
'reference') {
251 $tmparraykey = array();
252 $value = $tmparraykey[
GETPOST($html_name)] .
',' .
GETPOST($html_name .
'2');
254 if ($field_key ==
'lang') {
255 $value =
GETPOST($html_name,
'aZ09') ?
GETPOST($html_name,
'aZ09') :
"";
257 $value =
GETPOST($html_name,
'alphanohtml');
260 if (preg_match(
'/^integer:/i', $field_info[
'type']) && $value ==
'-1') {
263 if (!empty($field_info[
'foreignkey']) && $value ==
'-1') {
268 $field_value = $value;
269 if ($field_info[
'notnull'] > 0 && $field_value ==
'' && !is_null($field_info[
'default']) && $field_info[
'default'] ==
'(PROV)') {
270 $field_value =
'(PROV)';
271 } elseif ((!empty($field_info[
'required']) || $field_info[
'notnull'] > 0) && $field_value ==
'' && !empty($field_info[
'default'])) {
272 $field_value = $field_info[
'default'];
274 if ($field_info[
'notnull'] > 0 && $field_value ==
'' && is_null($field_info[
'default'])) {
276 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($field_info[
'label'])),
null,
'errors');
278 $deprecation_options[$mode_key][$field_key] = $field_value;
282 if (!$error && !empty($field_info[
'validate']) && is_callable(array($this,
'validateField'))) {
283 if (!$this->
validateField($mode_info[
'fields'], $field_key, $value)) {
291 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
292 if (!empty($mode_info[
'enabled_field'])) {
293 $info = explode(
':', $mode_info[
'enabled_field']);
294 if ($deprecation_options[$info[0]][$info[1]] != $info[2]) {
295 unset($deprecation_options[$info[0]][$info[1]]);
299 $this->deprecation_options = $deprecation_options;
317 global $langs, $hookmanager;
318 dol_syslog(__METHOD__ .
" asset_id=$asset_id, asset_model_id=$asset_model_id");
321 $this->errors = array();
322 $this->deprecation_options = array();
325 $asset_id = $asset_id > 0 ? $asset_id : 0;
326 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
328 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
329 $parameters = array(
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
330 $reshook = $hookmanager->executeHooks(
'fetchDepreciationOptions', $parameters, $this);
331 if (!empty($reshook)) {
336 if (empty($asset_id) && empty($asset_model_id)) {
337 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
345 $class_type = $asset_id > 0 ? 0 : 1;
346 $deprecation_options = array();
347 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
350 $result = $this->
fetchCommon(0,
'',
" AND " . ($asset_id > 0 ?
" fk_asset = " . (
int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id));
352 $this->errors = array_merge(array($langs->trans(
'AssetErrorFetchDepreciationOptionsForMode', $mode_key) .
':'), $this->errors);
354 } elseif ($result > 0) {
355 foreach ($this->fields as $field_key => $field_info) {
356 if (in_array($field_key, array(
'rowid',
'fk_asset',
'fk_asset_model',
'tms',
'fk_user_modif'))) {
359 $deprecation_options[$mode_key][$field_key] = $this->{$field_key};
364 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
365 if (!empty($mode_info[
'enabled_field'])) {
366 $info = explode(
':', $mode_info[
'enabled_field']);
367 if (isset($deprecation_options[$info[0]][$info[1]]) && $deprecation_options[$info[0]][$info[1]] != $info[2]) {
368 unset($deprecation_options[$info[0]][$info[1]]);
377 $this->deprecation_options = $deprecation_options;
393 $this->errors = array();
396 $mode = strtolower(trim($mode));
398 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
399 $parameters = array(
'mode' => $mode);
400 $reshook = $hookmanager->executeHooks(
'getGeneralDepreciationInfoForMode', $parameters, $this);
403 } elseif ($reshook > 0) {
404 return $hookmanager->resArray;
407 $duration_type_list = $this->deprecation_options_fields[$mode][
'fields'][
'duration_type'][
'arrayofkeyval'];
410 'base_depreciation_ht' => $this->deprecation_options[$mode][
'amount_base_depreciation_ht'],
411 'duration' => $this->deprecation_options[$mode][
'duration'],
412 'duration_type' => $duration_type_list[$this->deprecation_options[$mode][
'duration_type']],
413 'rate' => $this->
getRate($mode),
428 global $langs, $hookmanager;
429 dol_syslog(__METHOD__ .
" user_id=".$user->id.
", asset_id=".$asset_id.
", asset_model_id=".$asset_model_id.
", notrigger=".$notrigger);
432 $this->errors = array();
435 $asset_id = $asset_id > 0 ? $asset_id : 0;
436 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
438 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
439 $parameters = array(
'user' => $user,
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
440 $reshook = $hookmanager->executeHooks(
'updateDepreciationOptions', $parameters, $this);
441 if (!empty($reshook)) {
446 if (empty($asset_id) && empty($asset_model_id)) {
447 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
458 $this->fk_asset = $asset_id;
461 $this->fk_asset_model = $asset_model_id;
465 $this->fk_user_modif = $user->id;
467 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
469 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
470 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
471 $resql = $this->db->query($sql);
473 $this->errors[] = $langs->trans(
'AssetErrorDeleteDepreciationOptionsForMode', $mode_key) .
': ' . $this->db->lasterror();
477 if (!$error && !empty($this->deprecation_options[$mode_key])) {
478 if (!empty($mode_info[
'enabled_field'])) {
479 $info = explode(
':', $mode_info[
'enabled_field']);
480 if ($this->deprecation_options[$info[0]][$info[1]] != $info[2]) {
489 $this->errors = array_merge(array($langs->trans(
'AssetErrorInsertDepreciationOptionsForMode', $mode_key) .
':'), $this->errors);
495 if (!$error && $this->fk_asset > 0) {
497 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
498 $asset =
new Asset($this->db);
499 $result = $asset->fetch($this->fk_asset);
501 $result = $asset->calculationDepreciation();
504 $this->errors[] = $langs->trans(
'AssetErrorCalculationDepreciationLines');
505 $this->errors[] = $asset->errorsToString();
510 if (!$error && !$notrigger) {
512 $result = $this->
call_trigger(
'ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
520 $this->db->rollback();
536 $duration = (isset($this->deprecation_options[$mode][
"duration"]) && $this->deprecation_options[$mode][
"duration"] > 0) ? $this->deprecation_options[$mode][
"duration"] : 0;
537 $duration_type = (isset($this->deprecation_options[$mode][
"duration_type"]) && $this->deprecation_options[$mode][
"duration_type"] > 0) ? $this->deprecation_options[$mode][
"duration_type"] : 0;
539 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)
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.
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...
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.