dolibarr 25.0.0-alpha
asset.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018-2022 OpenDSI <support@open-dsi.fr>
3 * Copyright (C) 2022-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
33{
34 global $langs, $conf, $extrafields;
35
36 $extrafields->fetch_name_optionals_label('asset');
37 $extrafields->fetch_name_optionals_label('asset_model');
38
39 $langs->load("assets");
40
41 $h = 0;
42 $head = array();
43
44 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT.'/asset/admin/setup.php');
45 $head[$h][1] = $langs->trans("Settings");
46 $head[$h][2] = 'settings';
47 $h++;
48
49 // Show more tabs from modules
50 // Entries must be declared in modules descriptor with line
51 //$this->tabs = array(
52 // 'entity:+tabname:Title:@asset:/asset/mypage.php?id=__ID__'
53 //); // to add new tab
54 //$this->tabs = array(
55 // 'entity:-tabname:Title:@asset:/asset/mypage.php?id=__ID__'
56 //); // to remove a tab
57 complete_head_from_modules($conf, $langs, null, $head, $h, 'asset_admin');
58
59 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT.'/admin/extrafields.php', array('elementtype' => 'asset'));
60 $head[$h][1] = $langs->trans("ExtraFields");
61 $nbExtrafields = $extrafields->attributes['asset']['count'];
62 if ($nbExtrafields > 0) {
63 $head[$h][1] .= ' <span class="badge">'.$nbExtrafields.'</span>';
64 }
65 $head[$h][2] = 'asset_extrafields';
66 $h++;
67
68 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT.'/admin/extrafields.php', array('elementtype' => 'asset_model'));
69 $head[$h][1] = $langs->trans("ExtraFieldsAssetModel");
70 $nbExtrafields = $extrafields->attributes['asset_model']['count'];
71 if ($nbExtrafields > 0) {
72 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
73 }
74 $head[$h][2] = 'assetmodel_extrafields';
75 $h++;
76
77 complete_head_from_modules($conf, $langs, null, $head, $h, 'asset_admin', 'remove');
78
79 return $head;
80}
81
89{
90 global $db, $langs, $conf;
91
92 $langs->loadLangs(array("assets", "admin"));
93
94 $h = 0;
95 $head = array();
96
97 $head[$h][0] = DOL_URL_ROOT . '/asset/card.php?id=' . $object->id;
98 $head[$h][1] = $langs->trans("Asset");
99 $head[$h][2] = 'card';
100 $h++;
101
102 if (empty($object->not_depreciated)) {
103 $head[$h][0] = DOL_URL_ROOT . '/asset/depreciation_options.php?id=' . $object->id;
104 $head[$h][1] = $langs->trans("AssetDepreciationOptions");
105 $head[$h][2] = 'depreciation_options';
106 $h++;
107 }
108
109 $head[$h][0] = DOL_URL_ROOT . '/asset/accountancy_codes.php?id=' . $object->id;
110 $head[$h][1] = $langs->trans("AssetAccountancyCodes");
111 $head[$h][2] = 'accountancy_codes';
112 $h++;
113
114 if (empty($object->not_depreciated)) {
115 $head[$h][0] = DOL_URL_ROOT . '/asset/depreciation.php?id=' . $object->id;
116 $head[$h][1] = $langs->trans("AssetDepreciation");
117 $head[$h][2] = 'depreciation';
118 $h++;
119 }
120
121 if (isset($object->disposal_date) && $object->disposal_date !== "") {
122 $head[$h][0] = DOL_URL_ROOT . '/asset/disposal.php?id=' . $object->id;
123 $head[$h][1] = $langs->trans("AssetDisposal");
124 $head[$h][2] = 'disposal';
125 $h++;
126 }
127
128 if (isset($object->fields['note_public']) || isset($object->fields['note_private'])) {
129 $nbNote = 0;
130 if (!empty($object->note_private)) {
131 $nbNote++;
132 }
133 if (!empty($object->note_public)) {
134 $nbNote++;
135 }
136 $head[$h][0] = DOL_URL_ROOT . '/asset/note.php?id=' . $object->id;
137 $head[$h][1] = $langs->trans('Notes');
138 if ($nbNote > 0) {
139 $head[$h][1] .= (!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? '<span class="badge marginleftonlyshort">' . $nbNote . '</span>' : '');
140 }
141 $head[$h][2] = 'note';
142 $h++;
143 }
144
145 require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
146 require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
147 $upload_dir = $conf->asset->dir_output . "/asset/" . dol_sanitizeFileName($object->ref);
148 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
149 $nbLinks = Link::count($db, $object->element, $object->id);
150 $head[$h][0] = DOL_URL_ROOT . '/asset/document.php?id=' . $object->id;
151 $head[$h][1] = $langs->trans('Documents');
152 if (($nbFiles + $nbLinks) > 0) {
153 $head[$h][1] .= '<span class="badge marginleftonlyshort">' . ($nbFiles + $nbLinks) . '</span>';
154 }
155 $head[$h][2] = 'document';
156 $h++;
157
158 $head[$h][0] = DOL_URL_ROOT . '/asset/agenda.php?id=' . $object->id;
159 $head[$h][1] = $langs->trans("Events");
160 $head[$h][2] = 'agenda';
161 $h++;
162
163 // Show more tabs from modules
164 // Entries must be declared in modules descriptor with line
165 //$this->tabs = array(
166 // 'entity:+tabname:Title:@asset:/asset/mypage.php?id=__ID__'
167 //); // to add new tab
168 //$this->tabs = array(
169 // 'entity:-tabname:Title:@asset:/asset/mypage.php?id=__ID__'
170 //); // to remove a tab
171 complete_head_from_modules($conf, $langs, $object, $head, $h, 'asset');
172
173 complete_head_from_modules($conf, $langs, $object, $head, $h, 'asset', 'remove');
174
175 return $head;
176}
177
185{
186 global $langs, $conf;
187
188 $langs->loadLangs(array("assets", "admin"));
189
190 $h = 0;
191 $head = array();
192
193 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/asset/model/card.php', ['id' => $object->id]);
194 $head[$h][1] = $langs->trans("Card");
195 $head[$h][2] = 'card';
196 $h++;
197
198 if (isset($object->fields['note_public']) || isset($object->fields['note_private'])) {
199 $nbNote = 0;
200 if (!empty($object->note_private)) {
201 $nbNote++;
202 }
203 if (!empty($object->note_public)) {
204 $nbNote++;
205 }
206 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/asset/model/note.php', ['id' => $object->id]);
207 $head[$h][1] = $langs->trans('Notes');
208 if ($nbNote > 0) {
209 $head[$h][1] .= (!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? '<span class="badge marginleftonlyshort">' . $nbNote . '</span>' : '');
210 }
211 $head[$h][2] = 'note';
212 $h++;
213 }
214
215 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/asset/model/agenda.php', ['id' => $object->id]);
216 $head[$h][1] = $langs->trans("Events");
217 $head[$h][2] = 'agenda';
218 $h++;
219
220
221 // Show more tabs from modules
222 // Entries must be declared in modules descriptor with line
223 //$this->tabs = array(
224 // 'entity:+tabname:Title:@asset:/asset/mypage.php?id=__ID__'
225 //); // to add new tab
226 //$this->tabs = array(
227 // 'entity:-tabname:Title:@asset:/asset/mypage.php?id=__ID__'
228 //); // to remove a tab
229 complete_head_from_modules($conf, $langs, $object, $head, $h, 'assetmodel');
230
231 complete_head_from_modules($conf, $langs, $object, $head, $h, 'assetmodel', 'remove');
232
233 return $head;
234}
235
247{
248 return array(
249 'THIRTY_360' => 'AssetDayCountConventionThirty360', // Months of 30 days, year of 360 days (usual in France)
250 'ACT_365' => 'AssetDayCountConventionAct365', // Real calendar days, year of 365 days
251 'ACT_ACT' => 'AssetDayCountConventionActAct', // Real calendar days, each year divided by its own length (365 or 366)
252 );
253}
254
265{
266 global $mysoc;
267
268 $convention = getDolGlobalString('ASSET_DEPRECIATION_DAY_COUNT_CONVENTION');
269 if (array_key_exists($convention, getAssetDepreciationDayCountConventions())) {
270 return $convention;
271 }
272
273 // Backward compatibility with the deprecated ASSET_DEPRECIATION_DURATION_PER_YEAR
274 $nbdaysperyear = getDolGlobalInt('ASSET_DEPRECIATION_DURATION_PER_YEAR');
275 if ($nbdaysperyear == 360) {
276 return 'THIRTY_360';
277 } elseif ($nbdaysperyear >= 365) {
278 return 'ACT_365';
279 }
280
281 // Nothing set up at all: France computes depreciations on a 30/360 basis, other countries usually
282 // count real calendar days.
283 return (is_object($mysoc) && !empty($mysoc->country_code) && $mysoc->country_code == 'FR') ? 'THIRTY_360' : 'ACT_365';
284}
285
300function getAssetDepreciationPeriodFraction($timestampStart, $timestampEnd, $convention = '', $forcetimezone = '')
301{
302 if ($convention === '' || !array_key_exists($convention, getAssetDepreciationDayCountConventions())) {
303 // An unknown convention must not be computed as if it were one of the known ones: fall back on
304 // the convention of the installation, the same value the caller would have got with ''.
306 }
307 if ($timestampStart > $timestampEnd) {
308 return 0.0;
309 }
310
311 if ($convention == 'THIRTY_360') {
312 return num_between_day_30_360($timestampStart, $timestampEnd, 1, $forcetimezone) / 360;
313 }
314
315 if ($convention == 'ACT_ACT') {
316 // The period is split per calendar year, each part being divided by the real length of its own
317 // year (365 or 366), which is the only way to get an exact result on a period spanning a leap year.
318 $start = dol_getdate((int) $timestampStart, false, $forcetimezone);
319 $end = dol_getdate((int) $timestampEnd, false, $forcetimezone);
320
321 $daystart = $start['yday'] + 1; // 'yday' is 0 based
322 $dayend = $end['yday'] + 1;
323
324 if ($start['year'] == $end['year']) {
325 return ($dayend - $daystart + 1) / num_days_in_year($start['year']);
326 }
327
328 $fraction = (num_days_in_year($start['year']) - $daystart + 1) / num_days_in_year($start['year']);
329 for ($year = $start['year'] + 1; $year < $end['year']; $year++) {
330 $fraction += 1;
331 }
332 $fraction += $dayend / num_days_in_year($end['year']);
333
334 return $fraction;
335 }
336
337 // ACT_365
338 return num_between_day($timestampStart, $timestampEnd, 1) / 365;
339}
340
356function getAssetDepreciationMonthFraction($timestampStart, $timestampEnd, $convention = '', $forcetimezone = '')
357{
358 if ($convention === '' || !array_key_exists($convention, getAssetDepreciationDayCountConventions())) {
360 }
361 if ($timestampStart > $timestampEnd) {
362 return 0.0;
363 }
364
365 $start = dol_getdate((int) $timestampStart, false, $forcetimezone);
366 $end = dol_getdate((int) $timestampEnd, false, $forcetimezone);
367
368 if ($convention == 'THIRTY_360') {
369 // Every month is 30 days long, so the last day of a month counts as its 30th. Without that
370 // rule a full February would be worth 28/30 of a monthly amount instead of a whole one.
371 $lastdayofendmonth = dol_getdate(dol_get_last_day($end['year'], $end['mon'], true), false, $forcetimezone);
372 $dayend = ($end['mday'] >= $lastdayofendmonth['mday'] ? 30 : min($end['mday'], 30));
373 $daystart = min($start['mday'], 30);
374
375 return max(0.0, min(1.0, ($dayend - $daystart + 1) / 30));
376 }
377
378 // ACT_365 and ACT_ACT: real days divided by the real length of the month, so that both sides of
379 // the ratio belong to the same system.
380 $lastdayofstartmonth = dol_getdate(dol_get_last_day($start['year'], $start['mon'], true), false, $forcetimezone);
381 $nbdaysinmonth = (int) $lastdayofstartmonth['mday'];
382 if ($nbdaysinmonth <= 0) {
383 return 0.0;
384 }
385
386 return min(1.0, num_between_day($timestampStart, $timestampEnd, 1) / $nbdaysinmonth);
387}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
assetAdminPrepareHead()
Prepare admin pages header.
Definition asset.lib.php:32
assetPrepareHead(Asset $object)
Prepare array of tabs for Asset.
Definition asset.lib.php:88
assetModelPrepareHead($object)
Prepare array of tabs for AssetModel.
getAssetDepreciationDayCountConventions()
Return the list of the day count conventions available to compute the prorata temporis of a depreciat...
getAssetDepreciationPeriodFraction($timestampStart, $timestampEnd, $convention='', $forcetimezone='')
Return the fraction of a year (the prorata temporis) between two dates, for a given day count convent...
getAssetDepreciationDayCountConvention()
Return the day count convention to use to compute the prorata temporis of a depreciation.
getAssetDepreciationMonthFraction($timestampStart, $timestampEnd, $convention='', $forcetimezone='')
Return the fraction of a month covered by a period, for the given day count convention.
Class for Asset.
global $mysoc
num_days_in_year($year)
Function to return the real number of days of a year (365, or 366 for a leap year).
num_between_day_30_360($timestampStart, $timestampEnd, $lastday=0, $forcetimezone='')
Function to return number of days between two dates using the 30/360 day count convention: every mont...
num_between_day($timestampStart, $timestampEnd, $lastday=0)
Function to return number of days between two dates (date must be UTC date !) Example: 2012-01-01 201...
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:635
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:65
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.