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