dolibarr 21.0.0-alpha
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
31{
37 public $accountancy_codes_fields = array(
38 'economic' => array(
39 'label' => 'AssetAccountancyCodeDepreciationEconomic',
40 'table' => 'asset_accountancy_codes_economic',
41 'depreciation_debit' => 'depreciation_asset',
42 'depreciation_credit' => 'depreciation_expense',
43 'fields' => array(
44 'asset' => array('label' => 'AssetAccountancyCodeAsset'),
45 'depreciation_asset' => array('label' => 'AssetAccountancyCodeDepreciationAsset'),
46 'depreciation_expense' => array('label' => 'AssetAccountancyCodeDepreciationExpense'),
47 'value_asset_sold' => array('label' => 'AssetAccountancyCodeValueAssetSold'),
48 'receivable_on_assignment' => array('label' => 'AssetAccountancyCodeReceivableOnAssignment'),
49 'proceeds_from_sales' => array('label' => 'AssetAccountancyCodeProceedsFromSales'),
50 'vat_collected' => array('label' => 'AssetAccountancyCodeVatCollected'),
51 'vat_deductible' => array('label' => 'AssetAccountancyCodeVatDeductible','column_break' => true),
52 ),
53 ),
54 'accelerated_depreciation' => array(
55 'label' => 'AssetAccountancyCodeDepreciationAcceleratedDepreciation',
56 'table' => 'asset_accountancy_codes_fiscal',
57 'depreciation_debit' => 'accelerated_depreciation',
58 'depreciation_credit' => 'endowment_accelerated_depreciation',
59 'fields' => array(
60 'accelerated_depreciation' => array('label' => 'AssetAccountancyCodeAcceleratedDepreciation'),
61 'endowment_accelerated_depreciation' => array('label' => 'AssetAccountancyCodeEndowmentAcceleratedDepreciation'),
62 'provision_accelerated_depreciation' => array('label' => 'AssetAccountancyCodeProvisionAcceleratedDepreciation'),
63 ),
64 ),
65 );
66
70 public $accountancy_codes = array();
71
77 public function __construct(DoliDB $db)
78 {
79 $this->db = $db;
80 }
81
88 {
89 $this->accountancy_codes = array();
90 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
91 $this->accountancy_codes[$mode_key] = array();
92 foreach ($mode_info['fields'] as $field_key => $field_info) {
93 $accountancy_code = GETPOST($mode_key . '_' . $field_key, 'aZ09');
94 if (empty($accountancy_code) || $accountancy_code == '-1') {
95 $accountancy_code = '';
96 }
97 $this->accountancy_codes[$mode_key][$field_key] = $accountancy_code;
98 }
99 }
100 return $this->accountancy_codes;
101 }
102
110 public function fetchAccountancyCodes($asset_id = 0, $asset_model_id = 0)
111 {
112 global $langs, $hookmanager;
113 dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
114
115 $error = 0;
116 $this->errors = array();
117 $this->accountancy_codes = array();
118
119 // Clean parameters
120 $asset_id = $asset_id > 0 ? $asset_id : 0;
121 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
122
123 $hookmanager->initHooks(array('assetaccountancycodesdao'));
124 $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
125 $reshook = $hookmanager->executeHooks('fetchAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
126 if (!empty($reshook)) {
127 return $reshook;
128 }
129
130 // Check parameters
131 if (empty($asset_id) && empty($asset_model_id)) {
132 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
133 $error++;
134 }
135 if ($error) {
136 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
137 return -1;
138 }
139
140 $accountancy_codes = array();
141 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
142 $sql = "SELECT " . implode(',', array_keys($mode_info['fields']));
143 $sql .= " FROM " . MAIN_DB_PREFIX . $mode_info['table'];
144 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
145
146 $resql = $this->db->query($sql);
147 if ($resql) {
148 if ($obj = $this->db->fetch_object($resql)) {
149 $accountancy_codes[$mode_key] = array();
150 foreach ($mode_info['fields'] as $field_key => $field_info) {
151 $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
152 }
153 }
154 } else {
155 $this->errors[] = $langs->trans('AssetErrorFetchAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
156 $error++;
157 }
158 }
159
160 if ($error) {
161 dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
162 return -1;
163 } else {
164 $this->accountancy_codes = $accountancy_codes;
165 return 1;
166 }
167 }
168
178 public function updateAccountancyCodes($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
179 {
180 global $langs, $hookmanager;
181 dol_syslog(__METHOD__ . " user_id=".$user->id.", asset_id=".$asset_id.", asset_model_id=".$asset_model_id.", notrigger=".$notrigger);
182
183 $error = 0;
184 $this->errors = array();
185
186 // Clean parameters
187 $asset_id = $asset_id > 0 ? $asset_id : 0;
188 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
189
190 $hookmanager->initHooks(array('assetaccountancycodesdao'));
191 $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
192 $reshook = $hookmanager->executeHooks('updateAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
193 if (!empty($reshook)) {
194 return $reshook;
195 }
196
197 // Check parameters
198 if (empty($asset_id) && empty($asset_model_id)) {
199 $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
200 $error++;
201 }
202 if ($error) {
203 dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
204 return -1;
205 }
206
207 $this->db->begin();
208 $now = dol_now();
209
210 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
211 // Delete old accountancy codes
212 $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
213 $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
214 $resql = $this->db->query($sql);
215 if (!$resql) {
216 $this->errors[] = $langs->trans('AssetErrorDeleteAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
217 $error++;
218 }
219
220 if (!$error && !empty($this->accountancy_codes[$mode_key])) {
221 // Insert accountancy codes
222 $sql = "INSERT INTO " . MAIN_DB_PREFIX . $mode_info['table'] . "(";
223 $sql .= $asset_id > 0 ? "fk_asset," : "fk_asset_model,";
224 $sql .= implode(',', array_keys($mode_info['fields']));
225 $sql .= ", tms, fk_user_modif";
226 $sql .= ") VALUES(";
227 $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
228 foreach ($mode_info['fields'] as $field_key => $field_info) {
229 $sql .= ', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ? 'NULL' : "'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) . "'");
230 }
231 $sql .= ", '" . $this->db->idate($now) . "'";
232 $sql .= ", " . $user->id;
233 $sql .= ")";
234
235 $resql = $this->db->query($sql);
236 if (!$resql) {
237 $this->errors[] = $langs->trans('AssetErrorInsertAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
238 $error++;
239 }
240 }
241 }
242
243 if (!$error && $asset_id > 0) {
244 // Calculation of depreciation lines (reversal and future)
245 require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
246 $asset = new Asset($this->db);
247 $result = $asset->fetch($asset_id);
248 if ($result > 0) {
249 $result = $asset->calculationDepreciation();
250 }
251 if ($result < 0) {
252 $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
253 $this->errors[] = $asset->errorsToString();
254 $error++;
255 }
256 }
257
258 if (!$error && !$notrigger) {
259 // Call trigger
260 $result = $this->call_trigger('ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
261 if ($result < 0) {
262 $error++;
263 }
264 // End call triggers
265 }
266
267 if (!$error) {
268 $this->db->commit();
269 return 1;
270 } else {
271 $this->db->rollback();
272 return -1;
273 }
274 }
275}
Class for AssetAccountancyCodes.
updateAccountancyCodes($user, $asset_id=0, $asset_model_id=0, $notrigger=0)
Update accountancy codes of a asset or a asset model.
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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
dol_now($mode='auto')
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.