24require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
34 public $table_element =
'';
69 public $fields = array();
75 public $deprecation_options_fields = array(
77 'label' =>
'AssetDepreciationOptionEconomic',
78 'table' =>
'asset_depreciation_options_economic',
80 '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',),
81 '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'),
82 'duration' => array(
'type'=>
'integer',
'label'=>
'AssetDepreciationOptionDuration',
'enabled'=>
'1',
'position'=>30,
'notnull'=>1,
'visible'=>1,
'default'=>
'0',
'isameasure'=>
'1',
'validate'=>
'1',),
83 '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',),
84 '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")',),
85 'accelerated_depreciation_option' => array(
'type'=>
'boolean',
'label'=>
'AssetDepreciationOptionAcceleratedDepreciation',
'enabled'=>
'1',
'position'=>60,
'column_break' =>
true,
'notnull'=>0,
'default'=>
'0',
'visible'=>1,
'validate'=>
'1',),
86 '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',),
87 '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',),
88 '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 'accelerated_depreciation' => array(
92 'label' =>
'AssetDepreciationOptionAcceleratedDepreciation',
93 'table' =>
'asset_depreciation_options_fiscal',
94 'enabled_field' =>
'economic:accelerated_depreciation_option:1',
96 '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',),
97 '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'),
98 'duration' => array(
'type'=>
'integer',
'label'=>
'AssetDepreciationOptionDuration',
'enabled'=>
'1',
'position'=>30,
'notnull'=>1,
'visible'=>1,
'default'=>
'0',
'isameasure'=>
'1',
'validate'=>
'1',),
99 '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',),
100 '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")',),
101 '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',),
102 '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',),
103 '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',),
108 public $fk_asset_model;
110 public $fk_user_modif;
115 public $deprecation_options = array();
128 if (is_object($langs)) {
129 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
130 if (!empty($mode_info[
'fields']) && is_array($mode_info[
'fields'])) {
131 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
132 if (!empty($field_info[
'arrayofkeyval']) && is_array($field_info[
'arrayofkeyval'])) {
133 foreach ($field_info[
'arrayofkeyval'] as $key => $val) {
134 $this->deprecation_options_fields[$mode_key][
'fields'][$field_key][
'arrayofkeyval'][$key] = $langs->trans($val);
154 $mode = strtolower(trim($mode));
156 if (!empty($this->deprecation_options_fields[$mode])) {
157 $this->table_element = $this->deprecation_options_fields[$mode][
'table'];
158 $this->fields = $this->deprecation_options_fields[$mode][
'fields'];
159 foreach ($this->fields as $field_key => $field_info) {
160 if ((!empty($field_info[
'computed']) && !$all_field) || (!empty($field_info[
'only_on_asset']) && !empty($class_type))) {
161 unset($this->fields[$field_key]);
166 if (!empty($field_info[
'enabled_field'])) {
167 $info = explode(
':', $field_info[
'enabled_field']);
168 if ($this->deprecation_options[$info[0]][$info[1]] != $info[2] && isset($this->fields[$field_key][
'notnull'])) {
169 unset($this->fields[$field_key][
'notnull']);
173 $this->{$field_key} = $this->deprecation_options[$mode][$field_key];
176 $this->fields[
'rowid'] = array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>
'1',
'position'=>1,
'notnull'=>1,
'visible'=>0,
'noteditable'=>
'1',
'index'=>1,
'css'=>
'left',
'comment'=>
"Id");
177 if (empty($class_type)) {
178 $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',);
180 $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',);
182 $this->fields[
'tms'] = array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' =>
'1',
'position' => 501,
'notnull' => 0,
'visible' => 0,);
183 $this->fields[
'fk_user_modif'] = array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' =>
'1',
'position' => 511,
'notnull' => -1,
'visible' => 0,);
197 global $conf, $langs;
201 $deprecation_options = array();
202 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
205 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
206 if (!empty($field_info[
'computed'])) {
210 $html_name = $mode_key .
'_' . $field_key;
211 if ($field_info[
'type'] ==
'duration') {
212 if (
GETPOST($html_name .
'hour') ==
'' &&
GETPOST($html_name .
'min') ==
'') {
216 if (!GETPOSTISSET($html_name)) {
221 if (in_array($field_key, array(
'rowid',
'entity',
'import_key'))) {
224 if (in_array($field_key, array(
'date_creation',
'tms',
'fk_user_creat',
'fk_user_modif'))) {
225 if (!in_array(abs($field_info[
'visible']), array(1, 3))) {
231 if (in_array($field_info[
'type'], array(
'text',
'html'))) {
232 $value =
GETPOST($html_name,
'restricthtml');
233 } elseif ($field_info[
'type'] ==
'date') {
235 } elseif ($field_info[
'type'] ==
'datetime') {
236 $value =
dol_mktime(
GETPOST($html_name .
'hour',
'int'),
GETPOST($html_name .
'min',
'int'),
GETPOST($html_name .
'sec',
'int'),
GETPOST($html_name .
'month',
'int'),
GETPOST($html_name .
'day',
'int'),
GETPOST($html_name .
'year',
'int'),
'tzuserrel');
237 } elseif ($field_info[
'type'] ==
'duration') {
238 $value = 60 * 60 *
GETPOST($html_name .
'hour',
'int') + 60 *
GETPOST($html_name .
'min',
'int');
239 } elseif (preg_match(
'/^(integer|price|real|double)/', $field_info[
'type'])) {
241 } elseif ($field_info[
'type'] ==
'boolean') {
242 $value = ((
GETPOST($html_name) ==
'1' ||
GETPOST($html_name) ==
'on') ? 1 : 0);
243 } elseif ($field_info[
'type'] ==
'reference') {
245 $tmparraykey = array();
246 $value = $tmparraykey[
GETPOST($html_name)] .
',' .
GETPOST($html_name .
'2');
248 if ($field_key ==
'lang') {
249 $value =
GETPOST($html_name,
'aZ09') ?
GETPOST($html_name,
'aZ09') :
"";
251 $value =
GETPOST($html_name,
'alphanohtml');
254 if (preg_match(
'/^integer:/i', $field_info[
'type']) && $value ==
'-1') {
257 if (!empty($field_info[
'foreignkey']) && $value ==
'-1') {
262 $field_value = $value;
263 if ($field_info[
'notnull'] > 0 && $field_value ==
'' && !is_null($field_info[
'default']) && $field_info[
'default'] ==
'(PROV)') {
264 $field_value =
'(PROV)';
265 } elseif ((!empty($field_info[
'required']) || $field_info[
'notnull'] > 0) && $field_value ==
'' && !empty($field_info[
'default'])) {
266 $field_value =
dol_eval($field_info[
'default'], 1);
268 if ($field_info[
'notnull'] > 0 && $field_value ==
'' && is_null($field_info[
'default'])) {
270 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($field_info[
'label'])),
null,
'errors');
272 $deprecation_options[$mode_key][$field_key] = $field_value;
275 if ($conf->global->MAIN_FEATURE_LEVEL >= 2 || !empty($conf->global->MAIN_ACTIVATE_VALIDATION_RESULT)) {
276 if (!$error && !empty($field_info[
'validate']) && is_callable(array($this,
'validateField'))) {
277 if (!$this->
validateField($mode_info[
'fields'], $field_key, $value)) {
285 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
286 if (!empty($mode_info[
'enabled_field'])) {
287 $info = explode(
':', $mode_info[
'enabled_field']);
288 if ($deprecation_options[$info[0]][$info[1]] != $info[2]) {
289 unset($deprecation_options[$info[0]][$info[1]]);
293 $this->deprecation_options = $deprecation_options;
311 global $langs, $hookmanager;
312 dol_syslog(__METHOD__ .
" asset_id=$asset_id, asset_model_id=$asset_model_id");
315 $this->errors = array();
316 $this->deprecation_options = array();
319 $asset_id = $asset_id > 0 ? $asset_id : 0;
320 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
322 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
323 $parameters = array(
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
324 $reshook = $hookmanager->executeHooks(
'fetchDepreciationOptions', $parameters, $this);
325 if (!empty($reshook)) {
330 if (empty($asset_id) && empty($asset_model_id)) {
331 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
339 $class_type = $asset_id > 0 ? 0 : 1;
340 $deprecation_options = array();
341 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
344 $result = $this->
fetchCommon(0,
'',
" AND " . ($asset_id > 0 ?
" fk_asset = " . (
int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id));
346 $this->errors = array_merge(array($langs->trans(
'AssetErrorFetchDepreciationOptionsForMode', $mode_key) .
':'), $this->errors);
348 } elseif ($result > 0) {
349 foreach ($this->fields as $field_key => $field_info) {
350 if (in_array($field_key, array(
'rowid',
'fk_asset',
'fk_asset_model',
'tms',
'fk_user_modif')))
continue;
351 $deprecation_options[$mode_key][$field_key] = $this->{$field_key};
356 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
357 if (!empty($mode_info[
'enabled_field'])) {
358 $info = explode(
':', $mode_info[
'enabled_field']);
359 if ($deprecation_options[$info[0]][$info[1]] != $info[2]) {
360 unset($deprecation_options[$info[0]][$info[1]]);
369 $this->deprecation_options = $deprecation_options;
385 $this->errors = array();
388 $mode = strtolower(trim($mode));
390 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
391 $parameters = array(
'mode' => $mode);
392 $reshook = $hookmanager->executeHooks(
'getGeneralDepreciationInfoForMode', $parameters, $this);
395 } elseif ($reshook > 0) {
396 return $hookmanager->resArray;
399 $duration_type_list = $this->deprecation_options_fields[$mode][
'fields'][
'duration_type'][
'arrayofkeyval'];
402 'base_depreciation_ht' => $this->deprecation_options[$mode][
'amount_base_depreciation_ht'],
403 'duration' => $this->deprecation_options[$mode][
'duration'],
404 'duration_type' => $duration_type_list[$this->deprecation_options[$mode][
'duration_type']],
405 'rate' => $this->
getRate($mode),
420 global $langs, $hookmanager;
421 dol_syslog(__METHOD__ .
" user_id=".$user->id.
", asset_id=".$asset_id.
", asset_model_id=".$asset_model_id.
", notrigger=".$notrigger);
424 $this->errors = array();
427 $asset_id = $asset_id > 0 ? $asset_id : 0;
428 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
430 $hookmanager->initHooks(array(
'assetdepreciationoptionsdao'));
431 $parameters = array(
'user' => $user,
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
432 $reshook = $hookmanager->executeHooks(
'updateDepreciationOptions', $parameters, $this);
433 if (!empty($reshook)) {
438 if (empty($asset_id) && empty($asset_model_id)) {
439 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
450 $this->fk_asset = $asset_id;
453 $this->fk_asset_model = $asset_model_id;
457 $this->fk_user_modif = $user->id;
459 foreach ($this->deprecation_options_fields as $mode_key => $mode_info) {
461 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
462 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
463 $resql = $this->db->query($sql);
465 $this->errors[] = $langs->trans(
'AssetErrorDeleteDepreciationOptionsForMode', $mode_key) .
': ' . $this->db->lasterror();
469 if (!$error && !empty($this->deprecation_options[$mode_key])) {
470 if (!empty($mode_info[
'enabled_field'])) {
471 $info = explode(
':', $mode_info[
'enabled_field']);
472 if ($this->deprecation_options[$info[0]][$info[1]] != $info[2]) {
481 $this->errors = array_merge(array($langs->trans(
'AssetErrorInsertDepreciationOptionsForMode', $mode_key) .
':'), $this->errors);
487 if (!$error && $this->fk_asset > 0) {
489 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
490 $asset =
new Asset($this->db);
491 $result = $asset->fetch($this->fk_asset);
492 if ($result > 0) $result = $asset->calculationDepreciation();
494 $this->errors[] = $langs->trans(
'AssetErrorCalculationDepreciationLines');
495 $this->errors[] = $asset->errorsToString();
500 if (!$error && !$notrigger) {
502 $result = $this->
call_trigger(
'ASSET_DEPRECIATION_OPTIONS_MODIFY', $user);
510 $this->db->rollback();
526 $duration = $this->deprecation_options[$mode][
"duration"] > 0 ? $this->deprecation_options[$mode][
"duration"] : 0;
527 $duration_type = $this->deprecation_options[$mode][
"duration_type"] > 0 ? $this->deprecation_options[$mode][
"duration_type"] : 0;
529 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.
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
createCommon(User $user, $notrigger=false)
Create object into database.
errorsToString()
Method to output saved errors.
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 informations (by default a local PHP server timestamp) Re...
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.
dol_eval($s, $returnvalue=0, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.