dolibarr 23.0.3
assetaccountancycodes.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 Open-Dsi <support@open-dsi.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
28
33{
34 // TODO This class and table should not exists and should be properties of llx_asset_asset.
35
39 public $table_element = 'asset_accountancy_codes_economic';
40
44 public $fk_element = 'fk_asset';
45
46 // BEGIN MODULEBUILDER PROPERTIES
51 public $fields = array(
52 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => 'Id'),
53 //...
54 );
55
59 public $rowid;
60 // END MODULEBUILDER PROPERTIES
61
62
68 public $accountancy_codes_fields = array(
69 'economic' => array(
70 'label' => 'AssetAccountancyCodeDepreciationEconomic',
71 'table' => 'asset_accountancy_codes_economic',
72 'depreciation_debit' => 'depreciation_expense',
73 'depreciation_credit' => 'depreciation_asset',
74 'fields' => array(
75 'asset' => array('label' => 'AssetAccountancyCodeAsset'),
76 'depreciation_asset' => array('label' => 'AssetAccountancyCodeDepreciationAsset'),
77 'depreciation_expense' => array('label' => 'AssetAccountancyCodeDepreciationExpense'),
78 'value_asset_sold' => array('label' => 'AssetAccountancyCodeValueAssetSold'),
79 'receivable_on_assignment' => array('label' => 'AssetAccountancyCodeReceivableOnAssignment'),
80 'proceeds_from_sales' => array('label' => 'AssetAccountancyCodeProceedsFromSales'),
81 'vat_collected' => array('label' => 'AssetAccountancyCodeVatCollected'),
82 'vat_deductible' => array('label' => 'AssetAccountancyCodeVatDeductible','column_break' => true),
83 ),
84 ),
85 'accelerated_depreciation' => array(
86 'label' => 'AssetAccountancyCodeDepreciationAcceleratedDepreciation',
87 'table' => 'asset_accountancy_codes_fiscal',
88 'depreciation_debit' => 'endowment_accelerated_depreciation',
89 'depreciation_credit' => 'accelerated_depreciation',
90 'fields' => array(
91 'accelerated_depreciation' => array('label' => 'AssetAccountancyCodeAcceleratedDepreciation'),
92 'endowment_accelerated_depreciation' => array('label' => 'AssetAccountancyCodeEndowmentAcceleratedDepreciation'),
93 'provision_accelerated_depreciation' => array('label' => 'AssetAccountancyCodeProvisionAcceleratedDepreciation'),
94 ),
95 ),
96 );
97
101 public $fk_asset;
102
106 public $accountancy_codes = array();
107
113 public function __construct(DoliDB $db)
114 {
115 $this->db = $db;
116 }
117
125 public function fetch($id, $ref = null)
126 {
127 $result = $this->fetchCommon($id, $ref);
128
129 return $result;
130 }
131
139 public function delete(User $user, $notrigger = 0)
140 {
141 return $this->deleteCommon($user, $notrigger);
142 //return $this->deleteCommon($user, $notrigger, 1);
143 }
144
145
152 {
153 $this->accountancy_codes = array();
154 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
155 $this->accountancy_codes[$mode_key] = array();
156 foreach ($mode_info['fields'] as $field_key => $field_info) {
157 $accountancy_code = GETPOST($mode_key . '_' . $field_key, 'aZ09');
158 if (empty($accountancy_code) || $accountancy_code == '-1') {
159 $accountancy_code = '';
160 }
161 $this->accountancy_codes[$mode_key][$field_key] = $accountancy_code;
162 }
163 }
164 return $this->accountancy_codes;
165 }
166
174 public function fetchAccountancyCodes($asset_id = 0, $asset_model_id = 0)
175 {
176 global $langs, $hookmanager;
177 dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
178
179 $error = 0;
180 $this->errors = array();
181 $this->accountancy_codes = array();
182
183 // Clean parameters
184 $asset_id = $asset_id > 0 ? $asset_id : 0;
185 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
186
187 $hookmanager->initHooks(array('assetaccountancycodesdao'));
188 $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
189 $reshook = $hookmanager->executeHooks('fetchAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
190 if (!empty($reshook)) {
191 return $reshook;
192 }
193
194 // Check parameters
195 if (empty($asset_id) && empty($asset_model_id)) {
196 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
197 $error++;
198 }
199 if ($error) {
200 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
201 return -1;
202 }
203
204 $accountancy_codes = array();
205 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
206 $sql = "SELECT " . implode(',', array_keys($mode_info['fields']));
207 $sql .= " FROM " . MAIN_DB_PREFIX . $mode_info['table'];
208 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
209
210 $resql = $this->db->query($sql);
211 if ($resql) {
212 if ($obj = $this->db->fetch_object($resql)) {
213 $accountancy_codes[$mode_key] = array();
214 foreach ($mode_info['fields'] as $field_key => $field_info) {
215 $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
216 }
217 }
218 } else {
219 $this->errors[] = $langs->trans('AssetErrorFetchAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
220 $error++;
221 }
222 }
223
224 if ($error) {
225 dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
226 return -1;
227 } else {
228 $this->accountancy_codes = $accountancy_codes;
229 return 1;
230 }
231 }
232
242 public function updateAccountancyCodes($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
243 {
244 global $langs, $hookmanager;
245 dol_syslog(__METHOD__ . " user_id=".$user->id.", asset_id=".$asset_id.", asset_model_id=".$asset_model_id.", notrigger=".$notrigger);
246
247 $error = 0;
248 $this->errors = array();
249
250 // Clean parameters
251 $asset_id = $asset_id > 0 ? $asset_id : 0;
252 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
253
254 $hookmanager->initHooks(array('assetaccountancycodesdao'));
255 $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
256 $reshook = $hookmanager->executeHooks('updateAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
257 if (!empty($reshook)) {
258 return $reshook;
259 }
260
261 // Check parameters
262 if (empty($asset_id) && empty($asset_model_id)) {
263 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
264 $error++;
265 }
266 if ($error) {
267 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
268 return -1;
269 }
270
271 $this->db->begin();
272 $now = dol_now();
273
274 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
275 // Delete old accountancy codes
276 $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
277 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
278 $resql = $this->db->query($sql);
279 if (!$resql) {
280 $this->errors[] = $langs->trans('AssetErrorDeleteAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
281 $error++;
282 }
283
284 if (!$error && !empty($this->accountancy_codes[$mode_key])) {
285 // Insert accountancy codes
286 $sql = "INSERT INTO " . MAIN_DB_PREFIX . $mode_info['table'] . "(";
287 $sql .= $asset_id > 0 ? "fk_asset," : "fk_asset_model,";
288 $sql .= implode(',', array_keys($mode_info['fields']));
289 $sql .= ", tms, fk_user_modif";
290 $sql .= ") VALUES(";
291 $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
292 foreach ($mode_info['fields'] as $field_key => $field_info) {
293 $sql .= ', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ? 'NULL' : "'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) . "'");
294 }
295 $sql .= ", '" . $this->db->idate($now) . "'";
296 $sql .= ", " . $user->id;
297 $sql .= ")";
298
299 $resql = $this->db->query($sql);
300 if (!$resql) {
301 $this->errors[] = $langs->trans('AssetErrorInsertAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
302 $error++;
303 }
304 }
305 }
306
307 if (!$error && $asset_id > 0) {
308 // Calculation of depreciation lines (reversal and future)
309 require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
310 $asset = new Asset($this->db);
311 $result = $asset->fetch($asset_id);
312 if ($result > 0) {
313 $result = $asset->calculationDepreciation();
314 }
315 if ($result < 0) {
316 $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
317 $this->errors[] = $asset->errorsToString();
318 $error++;
319 }
320 }
321
322 if (!$error && !$notrigger) {
323 // Call trigger
324 $result = $this->call_trigger('ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
325 if ($result < 0) {
326 $error++;
327 }
328 // End call triggers
329 }
330
331 if (!$error) {
332 $this->db->commit();
333 return 1;
334 } else {
335 $this->db->rollback();
336 return -1;
337 }
338 }
339}
Class for AssetAccountancyCodes.
$fields
Array with all fields and their property.
updateAccountancyCodes($user, $asset_id=0, $asset_model_id=0, $notrigger=0)
Update accountancy codes of a asset or a asset model.
fetch($id, $ref=null)
Load object in memory from the database.
setAccountancyCodesFromPost()
Fill accountancy_codes property of object (using for data sent by forms)
__construct(DoliDB $db)
Constructor.
fetchAccountancyCodes($asset_id=0, $asset_model_id=0)
Load accountancy codes of a asset or a asset model.
Class for Asset.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
errorsToString()
Method to output saved errors.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.