dolibarr  17.0.4
html.formaccounting.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
3  * Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
4  * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
5  * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
6  * Copyright (C) 2016-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
27 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
28 
29 
33 class FormAccounting extends Form
34 {
35  private $options_cache = array();
36 
40  public $db;
41 
45  public $error = '';
46 
52  public function __construct($db)
53  {
54  $this->db = $db;
55  }
56 
57  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
72  public function select_journal($selectid, $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = 'maxwidth300 maxwidthonsmartphone', $usecache = '', $disabledajaxcombo = 0)
73  {
74  // phpcs:enable
75  global $conf, $langs;
76 
77  $out = '';
78 
79  $options = array();
80  if ($usecache && !empty($this->options_cache[$usecache])) {
81  $options = $this->options_cache[$usecache];
82  $selected = $selectid;
83  } else {
84  $sql = "SELECT rowid, code, label, nature, entity, active";
85  $sql .= " FROM ".$this->db->prefix()."accounting_journal";
86  $sql .= " WHERE active = 1";
87  $sql .= " AND entity = ".$conf->entity;
88  if ($nature && is_numeric($nature)) {
89  $sql .= " AND nature = ".((int) $nature);
90  }
91  $sql .= " ORDER BY code";
92 
93  dol_syslog(get_class($this)."::select_journal", LOG_DEBUG);
94  $resql = $this->db->query($sql);
95 
96  if (!$resql) {
97  $this->error = "Error ".$this->db->lasterror();
98  dol_syslog(get_class($this)."::select_journal ".$this->error, LOG_ERR);
99  return -1;
100  }
101 
102  $selected = 0;
103  $langs->load('accountancy');
104  while ($obj = $this->db->fetch_object($resql)) {
105  $label = $obj->code.' - '.$langs->trans($obj->label);
106 
107  $select_value_in = $obj->rowid;
108  $select_value_out = $obj->rowid;
109 
110  // Try to guess if we have found default value
111  if ($select_in == 1) {
112  $select_value_in = $obj->code;
113  }
114  if ($select_out == 1) {
115  $select_value_out = $obj->code;
116  }
117  // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
118  if ($selectid != '' && $selectid == $select_value_in) {
119  //var_dump("Found ".$selectid." ".$select_value_in);
120  $selected = $select_value_out;
121  }
122 
123  $options[$select_value_out] = $label;
124  }
125  $this->db->free($resql);
126 
127  if ($usecache) {
128  $this->options_cache[$usecache] = $options;
129  }
130  }
131 
132  $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, ($disabledajaxcombo ? 0 : 1));
133 
134  return $out;
135  }
136 
137  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
152  public function multi_select_journal($selectedIds = array(), $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = '', $usecache = '', $disabledajaxcombo = 0)
153  {
154  // phpcs:enable
155  global $conf, $langs;
156 
157  $out = '';
158 
159  $options = array();
160  if ($usecache && !empty($this->options_cache[$usecache])) {
161  $options = $this->options_cache[$usecache];
162  $selected = $selectedIds;
163  } else {
164  $sql = "SELECT rowid, code, label, nature, entity, active";
165  $sql .= " FROM ".$this->db->prefix()."accounting_journal";
166  $sql .= " WHERE active = 1";
167  $sql .= " AND entity = ".$conf->entity;
168  if ($nature && is_numeric($nature)) {
169  $sql .= " AND nature = ".((int) $nature);
170  }
171  $sql .= " ORDER BY code";
172 
173  dol_syslog(get_class($this)."::multi_select_journal", LOG_DEBUG);
174  $resql = $this->db->query($sql);
175 
176  if (!$resql) {
177  $this->error = "Error ".$this->db->lasterror();
178  dol_syslog(get_class($this)."::multi_select_journal ".$this->error, LOG_ERR);
179  return -1;
180  }
181 
182  $selected = array();
183  $langs->load('accountancy');
184  while ($obj = $this->db->fetch_object($resql)) {
185  $label = $langs->trans($obj->label);
186 
187  $select_value_in = $obj->rowid;
188  $select_value_out = $obj->rowid;
189 
190  // Try to guess if we have found default value
191  if ($select_in == 1) {
192  $select_value_in = $obj->code;
193  }
194  if ($select_out == 1) {
195  $select_value_out = $obj->code;
196  }
197  // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
198  if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
199  //var_dump("Found ".$selectid." ".$select_value_in);
200  $selected[] = $select_value_out;
201  }
202  $options[$select_value_out] = $label;
203  }
204  $this->db->free($resql);
205 
206  if ($usecache) {
207  $this->options_cache[$usecache] = $options;
208  }
209  }
210 
211  $out .= Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0, 'code_journal', '', ($disabledajaxcombo ? 0 : 1));
212 
213  return $out;
214  }
215 
216  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
229  public function select_accounting_category($selected = '', $htmlname = 'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
230  {
231  // phpcs:enable
232  global $db, $langs, $user, $mysoc;
233 
234  if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) {
235  dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
236  exit;
237  }
238 
239  if (!empty($mysoc->country_id)) {
240  $sql = "SELECT c.rowid, c.label as type, c.range_account";
241  $sql .= " FROM ".$this->db->prefix()."c_accounting_category as c";
242  $sql .= " WHERE c.active = 1";
243  $sql .= " AND c.category_type = 0";
244  if (empty($allcountries)) {
245  $sql .= " AND c.fk_country = ".((int) $mysoc->country_id);
246  }
247  $sql .= " ORDER BY c.label ASC";
248  } else {
249  $sql = "SELECT c.rowid, c.label as type, c.range_account";
250  $sql .= " FROM ".$this->db->prefix()."c_accounting_category as c, ".$this->db->prefix()."c_country as co";
251  $sql .= " WHERE c.active = 1";
252  $sql .= " AND c.category_type = 0";
253  $sql .= " AND c.fk_country = co.rowid";
254  if (empty($allcountries)) {
255  $sql .= " AND co.code = '".$this->db->escape($mysoc->country_code)."'";
256  }
257  $sql .= " ORDER BY c.label ASC";
258  }
259 
260  dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
261  $resql = $this->db->query($sql);
262  if ($resql) {
263  $num = $this->db->num_rows($resql);
264  if ($num) {
265  $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
266  $i = 0;
267 
268  if ($useempty) {
269  $out .= '<option value="0">&nbsp;</option>';
270  }
271  while ($i < $num) {
272  $obj = $this->db->fetch_object($resql);
273 
274  $titletoshowhtml = ($maxlen ? dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ? ' <span class="opacitymedium">('.$obj->range_account.')</span>' : '');
275  $titletoshow = ($maxlen ? dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ? ' ('.$obj->range_account.')' : '');
276 
277  $out .= '<option value="'.$obj->rowid.'"';
278  if ($obj->rowid == $selected) {
279  $out .= ' selected';
280  }
281  $out .= ' data-html="'.dol_escape_htmltag(dol_string_onlythesehtmltags($titletoshowhtml, 1, 1, 0, 0, array('span'))).'"';
282  $out .= '>';
283  $out .= dol_escape_htmltag($titletoshow);
284  $out .= '</option>';
285  $i++;
286  }
287  $out .= '</select>';
288  //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
289  } else {
290  $out = $langs->trans("ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code);
291  }
292  } else {
293  dol_print_error($this->db);
294  }
295 
296  $out .= ajax_combobox($htmlname, array());
297 
298  print $out;
299  }
300 
301  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
309  public function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '')
310  {
311  // phpcs:enable
312  $options = array();
313 
314  $sql = "SELECT DISTINCT import_key FROM ".$this->db->prefix()."accounting_bookkeeping";
315  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
316  $sql .= ' ORDER BY import_key DESC';
317 
318  dol_syslog(get_class($this)."::select_bookkeeping_importkey", LOG_DEBUG);
319  $resql = $this->db->query($sql);
320 
321  if (!$resql) {
322  $this->error = "Error ".$this->db->lasterror();
323  dol_syslog(get_class($this)."::select_bookkeeping_importkey ".$this->error, LOG_ERR);
324  return -1;
325  }
326 
327  while ($obj = $this->db->fetch_object($resql)) {
328  $options[$obj->import_key] = $obj->import_key;
329  }
330 
331  return Form::selectarray($htmlname, $options, $selectedkey);
332  }
333 
334  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
349  public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss = 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache = '', $active = '1')
350  {
351  // phpcs:enable
352  global $conf, $langs;
353 
354  require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
355 
356  $out = '';
357 
358  $options = array();
359 
360  if ($showempty == 2) {
361  $options['0'] = '--- '.$langs->trans("None").' ---';
362  }
363 
364  if ($usecache && !empty($this->options_cache[$usecache])) {
365  $options = $options + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
366  $selected = $selectid;
367  } else {
368  $trunclength = getDolGlobalInt('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
369 
370  $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
371  $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
372  $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
373  $sql .= " AND asy.rowid = ".((int) $conf->global->CHARTOFACCOUNTS);
374  if ($active === '1') {
375  $sql .= " AND aa.active = 1";
376  } elseif ($active === '0') {
377  $sql .= " AND aa.active = 0";
378  }
379  $sql .= " AND aa.entity=".((int) $conf->entity);
380  $sql .= " ORDER BY aa.account_number";
381 
382  dol_syslog(get_class($this)."::select_account", LOG_DEBUG);
383  $resql = $this->db->query($sql);
384 
385  if (!$resql) {
386  $this->error = "Error ".$this->db->lasterror();
387  dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR);
388  return -1;
389  }
390 
391  $num_rows = $this->db->num_rows($resql);
392 
393  if ($num_rows == 0 && (empty($conf->global->CHARTOFACCOUNTS) || $conf->global->CHARTOFACCOUNTS < 0)) {
394  $langs->load("errors");
395  $showempty = $langs->trans("ErrorYouMustFirstSetupYourChartOfAccount");
396  } else {
397  $selected = $selectid; // selectid can be -1, 0, 123
398  while ($obj = $this->db->fetch_object($resql)) {
399  if (empty($obj->labelshort)) {
400  $labeltoshow = $obj->label;
401  } else {
402  $labeltoshow = $obj->labelshort;
403  }
404 
405  $label = length_accountg($obj->account_number).' - '.$labeltoshow;
406  $label = dol_trunc($label, $trunclength);
407 
408  $select_value_in = $obj->rowid;
409  $select_value_out = $obj->rowid;
410 
411  // Try to guess if we have found default value
412  if ($select_in == 1) {
413  $select_value_in = $obj->account_number;
414  }
415  if ($select_out == 1) {
416  $select_value_out = $obj->account_number;
417  }
418  // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
419  // Because same account_number can be share between different accounting_system and do have the same meaning
420  if ($selectid != '' && $selectid == $select_value_in) {
421  //var_dump("Found ".$selectid." ".$select_value_in);
422  $selected = $select_value_out;
423  }
424 
425  $options[$select_value_out] = $label;
426  }
427  }
428 
429  $this->db->free($resql);
430 
431  if ($usecache) {
432  $this->options_cache[$usecache] = $options;
433  unset($this->options_cache[$usecache]['0']);
434  }
435  }
436 
437  $out .= Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
438 
439  return $out;
440  }
441 
442  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
454  public function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $morecss = 'maxwidth250', $usecache = '', $labelhtmlname = '')
455  {
456  // phpcs:enable
457 
458  $aux_account = array();
459 
460  if ($usecache && !empty($this->options_cache[$usecache])) {
461  $aux_account = $aux_account + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
462  } else {
463  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
464 
465  // Auxiliary thirdparties account
466  $sql = "SELECT code_compta, code_compta_fournisseur, nom as name";
467  $sql .= " FROM ".$this->db->prefix()."societe";
468  $sql .= " WHERE entity IN (".getEntity('societe').")";
469  $sql .= " AND (client IN (1,3) OR fournisseur = 1)";
470 
471  $resql = $this->db->query($sql);
472  if ($resql) {
473  while ($obj = $this->db->fetch_object($resql)) {
474  if (!empty($obj->code_compta)) {
475  $aux_account[$obj->code_compta] = $obj->code_compta.' <span class="opacitymedium">('.$obj->name.')</span>';
476  }
477  if (!empty($obj->code_compta_fournisseur)) {
478  $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' <span class="opacitymedium">('.$obj->name.')</span>';
479  }
480  }
481  } else {
482  $this->error = "Error ".$this->db->lasterror();
483  dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
484  return -1;
485  }
486 
487  ksort($aux_account);
488 
489  $this->db->free($resql);
490 
491  // Auxiliary user account
492  $sql = "SELECT DISTINCT accountancy_code, lastname, firstname ";
493  $sql .= " FROM ".$this->db->prefix()."user";
494  $sql .= " WHERE entity IN (".getEntity('user').")";
495  $sql .= " ORDER BY accountancy_code";
496 
497  $resql = $this->db->query($sql);
498  if ($resql) {
499  while ($obj = $this->db->fetch_object($resql)) {
500  if (!empty($obj->accountancy_code)) {
501  $aux_account[$obj->accountancy_code] = $obj->accountancy_code.' <span class="opacitymedium">('.dolGetFirstLastname($obj->firstname, $obj->lastname).')</span>';
502  }
503  }
504  } else {
505  $this->error = "Error ".$this->db->lasterror();
506  dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
507  return -1;
508  }
509  $this->db->free($resql);
510 
511  if ($usecache) {
512  $this->options_cache[$usecache] = $aux_account;
513  }
514  }
515 
516  // Build select
517  $out = '';
518  $out .= Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
519  //automatic filling if we give the name of the subledger_label input
520  if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
521  $out .= '<script>
522  jQuery(document).ready(() => {
523  $("#'.$htmlname.'").on("select2:select", function(e) {
524  var regExp = /\‍(([^)]+)\‍)/;
525  const match = regExp.exec(e.params.data.text);
526  $(\'input[name="'.dol_escape_js($labelhtmlname).'"]\').val(match[1]);
527  });
528  });
529 
530  </script>';
531  }
532 
533  return $out;
534  }
535 
536  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
546  public function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
547  {
548  // phpcs:enable
549  global $conf;
550 
551  $out_array = array();
552 
553  $sql = "SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
554  $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
555  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
556  $sql .= " ORDER BY date_format(doc_date, '%Y')";
557  dol_syslog(__METHOD__, LOG_DEBUG);
558  $resql = $this->db->query($sql);
559 
560  if (!$resql) {
561  $this->error = "Error ".$this->db->lasterror();
562  dol_syslog(__METHOD__.$this->error, LOG_ERR);
563  return -1;
564  }
565  while ($obj = $this->db->fetch_object($resql)) {
566  $out_array[$obj->dtyear] = $obj->dtyear;
567  }
568  $this->db->free($resql);
569 
570  if ($output_format == 'html') {
571  return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
572  } else {
573  return $out_array;
574  }
575  }
576 }
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:449
Class to manage generation of HTML components for accounting management.
select_bookkeeping_importkey($htmlname='importkey', $selectedkey='')
Return select filter with date of transaction.
__construct($db)
Constructor.
select_journal($selectid, $htmlname='journal', $nature=0, $showempty=0, $select_in=0, $select_out=0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='', $disabledajaxcombo=0)
Return list of journals with label by nature.
select_auxaccount($selectid, $htmlname='account_num_aux', $showempty=0, $morecss='maxwidth250', $usecache='', $labelhtmlname='')
Return list of auxilary accounts.
selectyear_accountancy_bookkepping($selected='', $htmlname='yearid', $useempty=0, $output_format='html')
Return HTML combo list of years existing into book keepping.
multi_select_journal($selectedIds=array(), $htmlname='journal', $nature=0, $showempty=0, $select_in=0, $select_out=0, $morecss='', $usecache='', $disabledajaxcombo=0)
Return list of journals with label by nature.
select_account($selectid, $htmlname='account', $showempty=0, $event=array(), $select_in=0, $select_out=0, $morecss='minwidth100 maxwidth300 maxwidthonsmartphone', $usecache='', $active='1')
Return list of accounts with label by chart of accounts.
select_accounting_category($selected='', $htmlname='account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
Return list of accounting category.
Class to manage generation of HTML components Only common components must be here.
static selectarray($htmlname, $array, $id='', $show_empty=0, $key_in_label=0, $value_as_key=0, $moreparam='', $translate=0, $maxlen=0, $disabled=0, $sort='', $morecss='minwidth75', $addjscombo=1, $moreparamonempty='', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
static multiselectarray($htmlname, $array, $selected=array(), $key_in_label=0, $value_as_key=0, $morecss='', $translate=0, $width=0, $moreattrib='', $elemtype='', $placeholder='', $addjscombo=-1)
Show a multiselect form from an array.
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_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_string_onlythesehtmltags($stringtoclean, $cleanalsosomestyles=1, $removeclassattribute=1, $cleanalsojavascript=0, $allowiframe=0, $allowed_tags=array())
Clean a string to keep only desirable HTML tags.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
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