dolibarr  18.0.6
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  return $this->accountancy_codes;
98  }
99 
107  public function fetchAccountancyCodes($asset_id = 0, $asset_model_id = 0)
108  {
109  global $langs, $hookmanager;
110  dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id");
111 
112  $error = 0;
113  $this->errors = array();
114  $this->accountancy_codes = array();
115 
116  // Clean parameters
117  $asset_id = $asset_id > 0 ? $asset_id : 0;
118  $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
119 
120  $hookmanager->initHooks(array('assetaccountancycodesdao'));
121  $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
122  $reshook = $hookmanager->executeHooks('fetchAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks
123  if (!empty($reshook)) {
124  return $reshook;
125  }
126 
127  // Check parameters
128  if (empty($asset_id) && empty($asset_model_id)) {
129  $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide');
130  $error++;
131  }
132  if ($error) {
133  dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR);
134  return -1;
135  }
136 
137  $accountancy_codes = array();
138  foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
139  $sql = "SELECT " . implode(',', array_keys($mode_info['fields']));
140  $sql .= " FROM " . MAIN_DB_PREFIX . $mode_info['table'];
141  $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
142 
143  $resql = $this->db->query($sql);
144  if ($resql) {
145  if ($obj = $this->db->fetch_object($resql)) {
146  $accountancy_codes[$mode_key] = array();
147  foreach ($mode_info['fields'] as $field_key => $field_info) {
148  $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
149  }
150  }
151  } else {
152  $this->errors[] = $langs->trans('AssetErrorFetchAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
153  $error++;
154  }
155  }
156 
157  if ($error) {
158  dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR);
159  return -1;
160  } else {
161  $this->accountancy_codes = $accountancy_codes;
162  return 1;
163  }
164  }
165 
175  public function updateAccountancyCodes($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0)
176  {
177  global $langs, $hookmanager;
178  dol_syslog(__METHOD__ . " user_id=".$user->id.", asset_id=".$asset_id.", asset_model_id=".$asset_model_id.", notrigger=".$notrigger);
179 
180  $error = 0;
181  $this->errors = 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('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id);
189  $reshook = $hookmanager->executeHooks('updateAccountancyCodes', $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  $this->db->begin();
205  $now = dol_now();
206 
207  foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
208  // Delete old accountancy codes
209  $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table'];
210  $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id);
211  $resql = $this->db->query($sql);
212  if (!$resql) {
213  $this->errors[] = $langs->trans('AssetErrorDeleteAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
214  $error++;
215  }
216 
217  if (!$error && !empty($this->accountancy_codes[$mode_key])) {
218  // Insert accountancy codes
219  $sql = "INSERT INTO " . MAIN_DB_PREFIX . $mode_info['table'] . "(";
220  $sql .= $asset_id > 0 ? "fk_asset," : "fk_asset_model,";
221  $sql .= implode(',', array_keys($mode_info['fields']));
222  $sql .= ", tms, fk_user_modif";
223  $sql .= ") VALUES(";
224  $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
225  foreach ($mode_info['fields'] as $field_key => $field_info) {
226  $sql .= ', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ? 'NULL' : "'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) . "'");
227  }
228  $sql .= ", '" . $this->db->idate($now) . "'";
229  $sql .= ", " . $user->id;
230  $sql .= ")";
231 
232  $resql = $this->db->query($sql);
233  if (!$resql) {
234  $this->errors[] = $langs->trans('AssetErrorInsertAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror();
235  $error++;
236  }
237  }
238  }
239 
240  if (!$error && $asset_id > 0) {
241  // Calculation of depreciation lines (reversal and future)
242  require_once DOL_DOCUMENT_ROOT . '/asset/class/asset.class.php';
243  $asset = new Asset($this->db);
244  $result = $asset->fetch($asset_id);
245  if ($result > 0) $result = $asset->calculationDepreciation();
246  if ($result < 0) {
247  $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines');
248  $this->errors[] = $asset->errorsToString();
249  $error++;
250  }
251  }
252 
253  if (!$error && !$notrigger) {
254  // Call trigger
255  $result = $this->call_trigger('ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
256  if ($result < 0) {
257  $error++;
258  }
259  // End call triggers
260  }
261 
262  if (!$error) {
263  $this->db->commit();
264  return 1;
265  } else {
266  $this->db->rollback();
267  return -1;
268  }
269  }
270 }
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.
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
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.