dolibarr  17.0.4
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 
24 require_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'),
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 
86  public function setAccountancyCodesFromPost()
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') $accountancy_code = '';
94  $this->accountancy_codes[$mode_key][$field_key] = $accountancy_code;
95  }
96  }
97  }
98 
106  public function fetchAccountancyCodes($asset_id = 0, $asset_model_id = 0)
107  {
108  global $langs, $hookmanager;
109  dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
110 
111  $error = 0;
112  $this->errors = array();
113  $this->accountancy_codes = array();
114 
115  // Clean parameters
116  $asset_id = $asset_id > 0 ? $asset_id : 0;
117  $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
118 
119  if (!is_object($hookmanager)) {
120  require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
121  $hookmanager = new HookManager($this->db);
122  }
123 
124  $hookmanager->initHooks(array('assetaccountancycodesdao'));
125  $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
126  $reshook = $hookmanager->executeHooks('fetchAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
127  if (!empty($reshook)) {
128  return $reshook;
129  }
130 
131  // Check parameters
132  if (empty($asset_id) && empty($asset_model_id)) {
133  $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
134  $error++;
135  }
136  if ($error) {
137  dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
138  return -1;
139  }
140 
141  $accountancy_codes = array();
142  foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
143  $sql = "SELECT " . implode(',', array_keys($mode_info['fields']));
144  $sql .= " FROM " . MAIN_DB_PREFIX . $mode_info['table'];
145  $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
146 
147  $resql = $this->db->query($sql);
148  if ($resql) {
149  if ($obj = $this->db->fetch_object($resql)) {
150  $accountancy_codes[$mode_key] = array();
151  foreach ($mode_info['fields'] as $field_key => $field_info) {
152  $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
153  }
154  }
155  } else {
156  $this->errors[] = $langs->trans('AssetErrorFetchAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
157  $error++;
158  }
159  }
160 
161  if ($error) {
162  dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
163  return -1;
164  } else {
165  $this->accountancy_codes = $accountancy_codes;
166  return 1;
167  }
168  }
169 
179  public function updateAccountancyCodes($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
180  {
181  global $langs, $hookmanager;
182  dol_syslog(__METHOD__ . " user_id={$user->id}, asset_id=$asset_id, asset_model_id=$asset_model_id, notrigger=$notrigger");
183 
184  $error = 0;
185  $this->errors = array();
186 
187  // Clean parameters
188  $asset_id = $asset_id > 0 ? $asset_id : 0;
189  $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
190 
191  if (!is_object($hookmanager)) {
192  require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
193  $hookmanager = new HookManager($this->db);
194  }
195 
196  $hookmanager->initHooks(array('assetaccountancycodesdao'));
197  $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
198  $reshook = $hookmanager->executeHooks('updateAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
199  if (!empty($reshook)) {
200  return $reshook;
201  }
202 
203  // Check parameters
204  if (empty($asset_id) && empty($asset_model_id)) {
205  $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
206  $error++;
207  }
208  if ($error) {
209  dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
210  return -1;
211  }
212 
213  $this->db->begin();
214  $now = dol_now();
215 
216  foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
217  // Delete old accountancy codes
218  $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
219  $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
220  $resql = $this->db->query($sql);
221  if (!$resql) {
222  $this->errors[] = $langs->trans('AssetErrorDeleteAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
223  $error++;
224  }
225 
226  if (!$error && !empty($this->accountancy_codes[$mode_key])) {
227  // Insert accountancy codes
228  $sql = "INSERT INTO " . MAIN_DB_PREFIX . $mode_info['table'] . "(";
229  $sql .= $asset_id > 0 ? "fk_asset," : "fk_asset_model,";
230  $sql .= implode(',', array_keys($mode_info['fields']));
231  $sql .= ", tms, fk_user_modif";
232  $sql .= ") VALUES(";
233  $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
234  foreach ($mode_info['fields'] as $field_key => $field_info) {
235  $sql .= ', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ? 'NULL' : "'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) . "'");
236  }
237  $sql .= ", '" . $this->db->idate($now) . "'";
238  $sql .= ", " . $user->id;
239  $sql .= ")";
240 
241  $resql = $this->db->query($sql);
242  if (!$resql) {
243  $this->errors[] = $langs->trans('AssetErrorInsertAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
244  $error++;
245  }
246  }
247  }
248 
249  if (!$error && $asset_id > 0) {
250  // Calculation of depreciation lines (reversal and future)
251  require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
252  $asset = new Asset($this->db);
253  $result = $asset->fetch($asset_id);
254  if ($result > 0) $result = $asset->calculationDepreciation();
255  if ($result < 0) {
256  $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
257  $this->errors[] = $asset->errorsToString();
258  $error++;
259  }
260  }
261 
262  if (!$error && !$notrigger) {
263  // Call trigger
264  $result = $this->call_trigger('ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
265  if ($result < 0) {
266  $error++;
267  }
268  // End call triggers
269  }
270 
271  if (!$error) {
272  $this->db->commit();
273  return 1;
274  } else {
275  $this->db->rollback();
276  return -1;
277  }
278  }
279 }
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.
Definition: asset.class.php:31
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.
Class to manage hooks.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
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.
$conf db
API class for accounts.
Definition: inc.php:41