626 $langs->load(
'assets');
629 $this->
id = $this->
id > 0 ? $this->id : 0;
630 $this->fk_asset_model = $this->fk_asset_model > 0 ? $this->fk_asset_model : 0;
634 if (empty($this->
id)) {
635 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Asset") .
' (' . $langs->transnoentitiesnoconv(
"TechnicalID") .
')');
638 if (empty($this->fk_asset_model)) {
639 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"AssetModel") .
' (' . $langs->transnoentitiesnoconv(
"TechnicalID") .
')');
650 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
652 $result = $options_model->fetchDeprecationOptions(0, $this->fk_asset_model);
654 $this->error = $options_model->error;
655 $this->errors = $options_model->errors;
657 } elseif ($result > 0) {
659 $result = $options->fetchDeprecationOptions($this->
id);
661 $this->error = $options->error;
662 $this->errors = $options->errors;
667 foreach ($options_model->deprecation_options as $mode_key => $fields) {
668 foreach ($fields as $field_key => $value) {
669 $options->deprecation_options[$mode_key][$field_key] = $value;
674 if (!array_key_exists(
'amount_base_depreciation_ht', $options->deprecation_options[$mode_key])) {
675 $options->deprecation_options[$mode_key][
'amount_base_depreciation_ht'] = $this->reversal_amount_ht > 0 ? $this->reversal_amount_ht : $this->acquisition_value_ht;
679 $result = $options->updateDeprecationOptions($user, $this->
id, 0, $notrigger);
681 $this->error = $options->error;
682 $this->errors = $options->errors;
691 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
693 $result = $accountancy_codes_model->fetchAccountancyCodes(0, $this->fk_asset_model);
695 $this->error = $accountancy_codes_model->error;
696 $this->errors = $accountancy_codes_model->errors;
698 } elseif ($result > 0) {
700 $result = $accountancy_codes->fetchAccountancyCodes($this->
id);
702 $this->error = $accountancy_codes->error;
703 $this->errors = $accountancy_codes->errors;
708 foreach ($accountancy_codes_model->accountancy_codes as $mode_key => $fields) {
709 foreach ($fields as $field_key => $value) {
710 $accountancy_codes->accountancy_codes[$mode_key][$field_key] = $value;
714 $result = $accountancy_codes->updateAccountancyCodes($user, $this->
id, 0, $notrigger);
716 $this->error = $accountancy_codes->error;
717 $this->errors = $accountancy_codes->errors;
725 $this->db->rollback();
913 global
$conf, $langs;
914 $langs->load(
'assets');
917 $this->
id = $this->
id > 0 ? $this->id : 0;
921 if (empty($this->
id)) {
922 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Asset") .
' (' . $langs->transnoentitiesnoconv(
"TechnicalID") .
')');
929 if (! empty($this->not_depreciated)) {
935 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
937 $result = $options->fetchDeprecationOptions($this->
id);
939 $this->error = $options->error;
940 $this->errors = $options->errors;
946 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
948 $result = $accountancy_codes->fetchAccountancyCodes($this->
id);
950 $this->error = $accountancy_codes->error;
951 $this->errors = $accountancy_codes->errors;
959 foreach ($options->deprecation_options as $mode_key => $fields) {
960 $modes[$mode_key] = $this->db->escape($mode_key);
962 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"asset_depreciation";
963 $sql .=
" WHERE fk_asset = " . (int) $this->
id;
964 $sql .=
" AND depreciation_mode NOT IN ('" . $this->db->sanitize(implode(
"', '", $modes)) .
"')";
966 $resql = $this->db->query($sql);
968 $this->errors[] = $langs->trans(
'AssetErrorClearDepreciationLines') .
': ' . $this->db->lasterror();
974 require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
975 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
979 $init_fiscal_period_start = $dates[
'date_start'];
980 $init_fiscal_period_end = $dates[
'date_end'];
990 foreach ($options->deprecation_options as $mode_key => $fields) {
1011 $last_depreciation_date =
'';
1012 $last_cumulative_depreciation_ht = $this->reversal_amount_ht;
1014 $sql =
"SELECT ad.depreciation_date, ad.cumulative_depreciation_ht";
1015 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation AS ad";
1016 $sql .=
" LEFT JOIN (SELECT DISTINCT fk_docdet FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping WHERE doc_type = 'asset') AS iab ON iab.fk_docdet = ad.rowid";
1017 $sql .=
" WHERE ad.fk_asset = " . (int) $this->
id;
1018 $sql .=
" AND ad.depreciation_mode = '" . $this->db->escape($mode_key) .
"'";
1019 $sql .=
" AND iab.fk_docdet IS NOT NULL";
1020 $sql .=
" ORDER BY ad.depreciation_date DESC";
1023 $resql = $this->db->query($sql);
1025 $this->errors[] = $langs->trans(
'AssetErrorFetchMaxDepreciationDateForMode', $mode_key) .
': ' . $this->db->lasterror();
1029 if ($obj = $this->db->fetch_object($resql)) {
1030 $last_depreciation_date = $this->db->jdate($obj->depreciation_date);
1031 $last_cumulative_depreciation_ht = $obj->cumulative_depreciation_ht;
1036 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->db->sanitize($options->deprecation_options_fields[$mode_key][
'table']);
1037 $sql .=
" SET total_amount_last_depreciation_ht = " . ((float) (empty($last_cumulative_depreciation_ht) ? 0 : $last_cumulative_depreciation_ht));
1038 $sql .=
" WHERE fk_asset = " . (int) $this->
id;
1039 $resql = $this->db->query($sql);
1041 $this->errors[] = $langs->trans(
'AssetErrorSetLastCumulativeDepreciation') .
': ' . $this->db->lasterror();
1047 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"asset_depreciation";
1048 $sql .=
" WHERE fk_asset = " . (int) $this->
id;
1049 $sql .=
" AND depreciation_mode = '" . $this->db->escape($mode_key) .
"'";
1051 $sql .=
" AND NOT EXISTS (SELECT fk_docdet FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping WHERE doc_type = 'asset' AND fk_docdet = " . MAIN_DB_PREFIX .
"asset_depreciation.rowid)";
1053 if ($last_depreciation_date !==
"") {
1054 $sql .=
" AND ref <> ''";
1056 $resql = $this->db->query($sql);
1058 $this->errors[] = $langs->trans(
'AssetErrorClearDepreciationLines') .
': ' . $this->db->lasterror();
1064 $depreciation_date_start = $this->date_start > $this->date_acquisition ? $this->date_start : $this->date_acquisition;
1065 $depreciation_date_end =
dol_time_plus_duree(
dol_time_plus_duree((
int) $depreciation_date_start, (
float) $fields[
'duration'], $fields[
'duration_type'] == 1 ?
'm' : ($fields[
'duration_type'] == 2 ?
'd' :
'y')), -1,
'd');
1066 $depreciation_amount = $fields[
'amount_base_depreciation_ht'];
1067 if ($fields[
'duration_type'] == 2) {
1068 $fiscal_period_start = $depreciation_date_start;
1069 $fiscal_period_end = $depreciation_date_start;
1070 } elseif ($fields[
'duration_type'] == 1) {
1071 $date_temp =
dol_getdate((
int) $depreciation_date_start);
1072 $fiscal_period_start =
dol_get_first_day($date_temp[
'year'], $date_temp[
'mon'],
false);
1073 $fiscal_period_end =
dol_get_last_day($date_temp[
'year'], $date_temp[
'mon'],
false);
1075 $fiscal_period_start = $init_fiscal_period_start;
1076 $fiscal_period_end = $init_fiscal_period_end;
1078 $cumulative_depreciation_ht = (float) $last_cumulative_depreciation_ht;
1079 $depreciation_period_amount = $depreciation_amount - (float) $this->reversal_amount_ht;
1080 $start_date = $depreciation_date_start;
1081 $disposal_date = isset($this->disposal_date) && $this->disposal_date !==
"" ? $this->disposal_date :
"";
1082 $finish_date = $disposal_date !==
"" ? $disposal_date : $depreciation_date_end;
1083 $accountancy_code_depreciation_debit_key = $accountancy_codes->accountancy_codes_fields[$mode_key][
'depreciation_debit'];
1084 $accountancy_code_depreciation_debit = $accountancy_codes->accountancy_codes[$mode_key][$accountancy_code_depreciation_debit_key] ??
'';
1085 $accountancy_code_depreciation_credit_key = $accountancy_codes->accountancy_codes_fields[$mode_key][
'depreciation_credit'];
1086 $accountancy_code_credit = $accountancy_codes->accountancy_codes[$mode_key][$accountancy_code_depreciation_credit_key] ??
'';
1090 if ($last_depreciation_date ===
"" && ($depreciation_date_start < $fiscal_period_start || is_numeric($this->reversal_date))) {
1091 if (is_numeric($this->reversal_date)) {
1092 if ($this->reversal_date < $fiscal_period_start) {
1093 $this->errors[] = $langs->trans(
'AssetErrorReversalDateNotGreaterThanCurrentBeginFiscalDateForMode', $mode_key);
1098 if (empty($this->reversal_amount_ht)) {
1099 $this->errors[] = $langs->trans(
'AssetErrorReversalAmountNotProvidedForMode', $mode_key);
1104 $start_date = $this->reversal_date;
1105 $result = $this->
addDepreciationLine($mode_key,
'', $start_date, (
float) $this->reversal_amount_ht, (
float) $this->reversal_amount_ht, $accountancy_code_depreciation_debit, $accountancy_code_credit);
1111 $this->errors[] = $langs->trans(
'AssetErrorReversalDateNotProvidedForMode', $mode_key);
1120 $period_amount = (float) ($fields[
'duration'] > 0 ?
price2num($depreciation_period_amount / $fields[
'duration'],
'MT') : 0);
1121 $first_period_found =
false;
1126 $is_degressive = ($fields[
'depreciation_type'] == 1 && (float) $fields[
'degressive_coefficient'] > 0 && $fields[
'duration'] > 0);
1127 if ($is_degressive && $fields[
'duration_type'] == 2) {
1128 $this->errors[] = $langs->trans(
'AssetErrorDegressiveDailyDurationForMode', $mode_key);
1132 $degressive_rate = $is_degressive ? (float) $fields[
'degressive_coefficient'] * ($fields[
'duration_type'] == 1 ? 12 : 1) / $fields[
'duration'] : 0;
1133 $degressive_year_end =
null;
1134 $degressive_year_rate = 0;
1135 $degressive_year_base = 0;
1138 $first_period_date = $fiscal_period_start;
1140 $ref_date_format =
"%Y" . ($fields[
'duration_type'] == 1 || $fields[
'duration_type'] == 2 ?
'-%m' :
'') . ($fields[
'duration_type'] == 2 ?
'-%d' :
'');
1144 $max_loop = $fields[
'duration'] + 2;
1148 if ($idx_loop > $max_loop) {
1152 if ($last_depreciation_date < $fiscal_period_end && ($first_period_date <= $start_date || $first_period_found)) {
1154 if ($fiscal_period_start <= $disposal_date && $disposal_date <= $fiscal_period_end && empty($this->disposal_depreciated)) {
1158 $first_period_found =
true;
1160 $period_begin =
dol_print_date($fiscal_period_start, $ref_date_format);
1161 $period_end =
dol_print_date($fiscal_period_end, $ref_date_format);
1162 $ref = $period_begin . ($period_begin != $period_end ?
' - ' . $period_end :
'');
1163 if ($fiscal_period_start <= $disposal_date && $disposal_date <= $fiscal_period_end) {
1164 $ref .=
' - ' . $langs->transnoentitiesnoconv(
'AssetDisposal');
1167 $begin_date = $fiscal_period_start < $start_date && $start_date <= $fiscal_period_end ? $start_date : $fiscal_period_start;
1168 $end_date = $fiscal_period_start < $finish_date && $finish_date <= $fiscal_period_end ? $finish_date : $fiscal_period_end;
1169 if ($fields[
'duration_type'] == 2) {
1170 $depreciation_ht = $period_amount;
1171 } elseif ($fields[
'duration_type'] == 1) {
1178 if ($is_degressive) {
1180 if ($degressive_year_end ===
null || $fiscal_period_start > $degressive_year_end) {
1181 $degressive_year_end = ($degressive_year_end ===
null ? $init_fiscal_period_end : $degressive_year_end);
1182 while ($fiscal_period_start > $degressive_year_end) {
1184 $degressive_year_end = ($next_fy !==
null ? $next_fy[
'date_end'] :
dol_time_plus_duree($degressive_year_end, 1,
'y'));
1187 $degressive_year_rate = max($degressive_rate, $remaining_years > 0 ? 1 / $remaining_years : 1);
1188 $degressive_year_base = $depreciation_amount - (float) $cumulative_depreciation_ht;
1190 $depreciation_ht = (float)
price2num(min($depreciation_amount - (
float) $cumulative_depreciation_ht, $degressive_year_base * $degressive_year_rate / 12 * $period_fraction),
'MT');
1192 $depreciation_ht = (float)
price2num($period_amount * $period_fraction,
'MT');
1198 $nb_days_fiscal_period =
num_between_day($fiscal_period_start, $fiscal_period_end, 1);
1199 $is_standard_fiscal_period = ($nb_days_fiscal_period >= 365 && $nb_days_fiscal_period <= 366);
1201 if ($is_standard_fiscal_period && $begin_date <= $fiscal_period_start && $end_date >= $fiscal_period_end) {
1206 $period_fraction = 1;
1216 if ($is_standard_fiscal_period) {
1218 $period_fraction = min(1, $period_fraction);
1221 if ($is_degressive) {
1222 $remaining_value = $depreciation_amount - (float) $cumulative_depreciation_ht;
1224 $year_rate = max($degressive_rate, $remaining_years > 0 ? 1 / $remaining_years : 1);
1225 $depreciation_ht = (float)
price2num(min($remaining_value, $remaining_value * $year_rate * $period_fraction),
'MT');
1227 $depreciation_ht = (float)
price2num($period_amount * $period_fraction,
'MT');
1231 if ($idx_loop < $max_loop) {
1232 $depreciation_ht = ceil($depreciation_ht);
1236 if ($fiscal_period_start <= $depreciation_date_end && $depreciation_date_end <= $fiscal_period_end) {
1237 $depreciation_ht = (float)
price2num($depreciation_amount - (
float) $cumulative_depreciation_ht,
'MT');
1238 $cumulative_depreciation_ht = $depreciation_amount;
1240 $cumulative_depreciation_ht += $depreciation_ht;
1243 $result = $this->
addDepreciationLine($mode_key, $ref, $fiscal_period_end, $depreciation_ht, (
float) $cumulative_depreciation_ht, $accountancy_code_depreciation_debit, $accountancy_code_credit);
1253 if ($fields[
'duration_type'] == 0) {
1257 if ($next_fy !==
null) {
1259 $fiscal_period_start = $next_fy[
'date_start'];
1260 $fiscal_period_end = $next_fy[
'date_end'];
1264 $current_fy_start_parts =
dol_getdate($fiscal_period_start);
1272 } elseif ($fields[
'duration_type'] == 1) {
1275 $fiscal_period_end = $fiscal_period_start;
1277 $last_period_date = $disposal_date !==
"" && $disposal_date < $depreciation_date_end ? $disposal_date : $depreciation_date_end;
1278 }
while ($fiscal_period_start < $last_period_date);
1287 $this->db->rollback();
1290 $this->db->commit();
1501 public function getNomUrl($withpicto = 0, $option =
'', $maxlen = 0, $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1503 global $db,
$conf, $langs, $hookmanager;
1504 global $dolibarr_main_authentication, $dolibarr_main_demo;
1505 global $menumanager;
1507 if (!empty(
$conf->dol_no_mouse_hover)) {
1513 $label = img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Asset").
'</u>';
1514 if (isset($this->
status)) {
1515 $label .=
' '.$this->getLibStatut(5);
1518 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1520 $url =
dol_buildpath(
'/asset/card.php', 1).
'?id='.$this->id;
1522 if ($option !=
'nolink') {
1524 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1525 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1526 $add_save_lastsearch_values = 1;
1528 if ($add_save_lastsearch_values) {
1529 $url .=
'&save_lastsearch_values=1';
1534 if (empty($notooltip)) {
1536 $label = $langs->trans(
"ShowAsset");
1537 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1539 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
1540 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1542 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1545 if ($option ==
'nolink') {
1546 $linkstart =
'<span';
1548 $linkstart =
'<a href="'.$url.
'"';
1550 $linkstart .= $linkclose.
'>';
1551 if ($option ==
'nolink') {
1552 $linkend =
'</span>';
1557 $result .= $linkstart;
1559 if (empty($this->showphoto_on_popup)) {
1561 $result .= img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1565 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1567 list($class, $module) = explode(
'@', $this->picto);
1570 $filename = $filearray[0][
'name'];
1571 if (!empty($filename)) {
1572 $pospoint = strpos($filearray[0][
'name'],
'.');
1574 $pathtophoto = $class.
'/'.$this->
ref.
'/thumbs/'.
dol_substr($filename, 0, $pospoint).
'_mini'.
dol_substr($filename, $pospoint);
1575 if (!
getDolGlobalString(strtoupper($module.
'_'.$class).
'_FORMATLISTPHOTOSASUSERS')) {
1576 $result .=
'<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo'.$module.
'" alt="No photo" border="0" src="'.DOL_URL_ROOT.
'/viewimage.php?modulepart='.$module.
'&entity='.
$conf->entity.
'&file='.urlencode($pathtophoto).
'"></div></div>';
1578 $result .=
'<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="'.DOL_URL_ROOT.
'/viewimage.php?modulepart='.$module.
'&entity='.
$conf->entity.
'&file='.urlencode($pathtophoto).
'"></div>';
1581 $result .=
'</div>';
1583 $result .= img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1588 if ($withpicto != 2) {
1590 if ($option ==
'label') {
1591 $name = $this->label;
1592 } elseif ($option ==
'with_label') {
1593 $name .=
' - ' . $this->label;
1598 $result .= $linkend;
1602 $hookmanager->initHooks(array($this->element .
'dao'));
1603 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1604 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1606 $result = $hookmanager->resPrint;
1608 $result .= $hookmanager->resPrint;