dolibarr  13.0.2
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 
36  private $options_cache = array();
37 
41  public $db;
42 
46  public $error = '';
47 
53  public function __construct($db)
54  {
55  $this->db = $db;
56  }
57 
58  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
73  public function select_journal($selectid, $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = 'maxwidth300 maxwidthonsmartphone', $usecache = '', $disabledajaxcombo = 0)
74  {
75  // phpcs:enable
76  global $conf, $langs;
77 
78  $out = '';
79 
80  $options = array();
81  if ($usecache && !empty($this->options_cache[$usecache]))
82  {
83  $options = $this->options_cache[$usecache];
84  $selected = $selectid;
85  } else {
86  $sql = "SELECT rowid, code, label, nature, entity, active";
87  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_journal";
88  $sql .= " WHERE active = 1";
89  $sql .= " AND entity = ".$conf->entity;
90  if ($nature && is_numeric($nature)) $sql .= " AND nature = ".$nature;
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  {
106  $label = $obj->code.' - '.$langs->trans($obj->label);
107 
108  $select_value_in = $obj->rowid;
109  $select_value_out = $obj->rowid;
110 
111  // Try to guess if we have found default value
112  if ($select_in == 1) {
113  $select_value_in = $obj->code;
114  }
115  if ($select_out == 1) {
116  $select_value_out = $obj->code;
117  }
118  // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
119  if ($selectid != '' && $selectid == $select_value_in) {
120  //var_dump("Found ".$selectid." ".$select_value_in);
121  $selected = $select_value_out;
122  }
123 
124  $options[$select_value_out] = $label;
125  }
126  $this->db->free($resql);
127 
128  if ($usecache)
129  {
130  $this->options_cache[$usecache] = $options;
131  }
132  }
133 
134  $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, ($disabledajaxcombo ? 0 : 1));
135 
136  return $out;
137  }
138 
139  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
152  public function select_accounting_category($selected = '', $htmlname = 'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
153  {
154  // phpcs:enable
155  global $db, $langs, $user, $mysoc;
156 
157  if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries))
158  {
159  dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
160  exit;
161  }
162 
163  if (!empty($mysoc->country_id))
164  {
165  $sql = "SELECT c.rowid, c.label as type, c.range_account";
166  $sql .= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c";
167  $sql .= " WHERE c.active = 1";
168  $sql .= " AND c.category_type = 0";
169  if (empty($allcountries)) $sql .= " AND c.fk_country = ".$mysoc->country_id;
170  $sql .= " ORDER BY c.label ASC";
171  } else {
172  $sql = "SELECT c.rowid, c.label as type, c.range_account";
173  $sql .= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c, ".MAIN_DB_PREFIX."c_country as co";
174  $sql .= " WHERE c.active = 1";
175  $sql .= " AND c.category_type = 0";
176  $sql .= " AND c.fk_country = co.rowid";
177  if (empty($allcountries)) $sql .= " AND co.code = '".$this->db->escape($mysoc->country_code)."'";
178  $sql .= " ORDER BY c.label ASC";
179  }
180 
181  dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
182  $resql = $this->db->query($sql);
183  if ($resql)
184  {
185  $num = $this->db->num_rows($resql);
186  if ($num)
187  {
188  $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
189  $i = 0;
190 
191  if ($useempty) $out .= '<option value="0">&nbsp;</option>';
192  while ($i < $num)
193  {
194  $obj = $this->db->fetch_object($resql);
195  $out .= '<option value="'.$obj->rowid.'"';
196  if ($obj->rowid == $selected) $out .= ' selected';
197  $out .= '>'.($maxlen ? dol_trunc($obj->type, $maxlen) : $obj->type);
198  $out .= ' ('.$obj->range_account.')';
199  $i++;
200  }
201  $out .= '</select>';
202  //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
203  } else {
204  $out .= $langs->trans("ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code);
205  }
206  } else {
207  dol_print_error($this->db);
208  }
209 
210  $out .= ajax_combobox($htmlname, array());
211 
212  print $out;
213  }
214 
215  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
223  public function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '')
224  {
225  // phpcs:enable
226  $options = array();
227 
228  $sql = 'SELECT DISTINCT import_key from '.MAIN_DB_PREFIX.'accounting_bookkeeping';
229  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
230  $sql .= ' ORDER BY import_key DESC';
231 
232  dol_syslog(get_class($this)."::select_bookkeeping_importkey", LOG_DEBUG);
233  $resql = $this->db->query($sql);
234 
235  if (!$resql) {
236  $this->error = "Error ".$this->db->lasterror();
237  dol_syslog(get_class($this)."::select_bookkeeping_importkey ".$this->error, LOG_ERR);
238  return -1;
239  }
240 
241  while ($obj = $this->db->fetch_object($resql)) {
242  $options[$obj->import_key] = $obj->import_key;
243  }
244 
245  return Form::selectarray($htmlname, $options, $selectedkey);
246  }
247 
248  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
262  public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss = 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache = '')
263  {
264  // phpcs:enable
265  global $conf, $langs;
266 
267  require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
268 
269  $out = '';
270 
271  $options = array();
272 
273  if ($showempty == 2)
274  {
275  $options['0'] = '--- '.$langs->trans("None").' ---';
276  }
277 
278  if ($usecache && !empty($this->options_cache[$usecache]))
279  {
280  $options = $options + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
281  $selected = $selectid;
282  } else {
283  $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT) ? 50 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT;
284 
285  $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
286  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa";
287  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
288  $sql .= " AND asy.rowid = ".$conf->global->CHARTOFACCOUNTS;
289  $sql .= " AND aa.active = 1";
290  $sql .= " AND aa.entity=".$conf->entity;
291  $sql .= " ORDER BY aa.account_number";
292 
293  dol_syslog(get_class($this)."::select_account", LOG_DEBUG);
294  $resql = $this->db->query($sql);
295 
296  if (!$resql) {
297  $this->error = "Error ".$this->db->lasterror();
298  dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR);
299  return -1;
300  }
301 
302  $num_rows = $this->db->num_rows($resql);
303 
304  if ($num_rows == 0) {
305  $langs->load("errors");
306  $showempty = $langs->trans("ErrorYouMustFirstSetupYourChartOfAccount");
307  } else {
308  $selected = $selectid; // selectid can be -1, 0, 123
309  while ($obj = $this->db->fetch_object($resql)) {
310  if (empty($obj->labelshort))
311  {
312  $labeltoshow = $obj->label;
313  } else {
314  $labeltoshow = $obj->labelshort;
315  }
316 
317  $label = length_accountg($obj->account_number).' - '.$labeltoshow;
318  $label = dol_trunc($label, $trunclength);
319 
320  $select_value_in = $obj->rowid;
321  $select_value_out = $obj->rowid;
322 
323  // Try to guess if we have found default value
324  if ($select_in == 1) {
325  $select_value_in = $obj->account_number;
326  }
327  if ($select_out == 1) {
328  $select_value_out = $obj->account_number;
329  }
330  // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
331  // Because same account_number can be share between different accounting_system and do have the same meaning
332  if ($selectid != '' && $selectid == $select_value_in) {
333  //var_dump("Found ".$selectid." ".$select_value_in);
334  $selected = $select_value_out;
335  }
336 
337  $options[$select_value_out] = $label;
338  }
339  }
340 
341  $this->db->free($resql);
342 
343  if ($usecache)
344  {
345  $this->options_cache[$usecache] = $options;
346  unset($this->options_cache[$usecache]['0']);
347  }
348  }
349 
350  $out .= Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
351 
352  return $out;
353  }
354 
355  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
365  public function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $morecss = 'maxwidth200')
366  {
367  // phpcs:enable
368 
369  $aux_account = array();
370 
371  // Auxiliary customer account
372  $sql = "SELECT DISTINCT code_compta, nom ";
373  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
374  $sql .= " WHERE entity IN (".getEntity('societe').")";
375  $sql .= " AND client IN (1 ,3)"; // only type customer or type customer/prospect
376  $sql .= " ORDER BY code_compta";
377 
378  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
379  $resql = $this->db->query($sql);
380  if ($resql) {
381  while ($obj = $this->db->fetch_object($resql)) {
382  if (!empty($obj->code_compta)) {
383  $aux_account[$obj->code_compta] = $obj->code_compta.' <span class="opacitymedium">('.$obj->nom.')</span>';
384  }
385  }
386  } else {
387  $this->error = "Error ".$this->db->lasterror();
388  dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
389  return -1;
390  }
391  $this->db->free($resql);
392 
393  // Auxiliary supplier account
394  $sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
395  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
396  $sql .= " WHERE entity IN (".getEntity('societe').")";
397  $sql .= " AND fournisseur = 1"; // only type supplier
398  $sql .= " ORDER BY code_compta_fournisseur";
399  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
400  $resql = $this->db->query($sql);
401  if ($resql) {
402  while ($obj = $this->db->fetch_object($resql)) {
403  if ($obj->code_compta_fournisseur != "") {
404  $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' <span class="opacitymedium">('.$obj->nom.')</span>';
405  }
406  }
407  } else {
408  $this->error = "Error ".$this->db->lasterror();
409  dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
410  return -1;
411  }
412  $this->db->free($resql);
413 
414  // Auxiliary user account
415  $sql = "SELECT DISTINCT accountancy_code, lastname, firstname ";
416  $sql .= " FROM ".MAIN_DB_PREFIX."user";
417  $sql .= " WHERE entity IN (".getEntity('user').")";
418  $sql .= " ORDER BY accountancy_code";
419  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
420  $resql = $this->db->query($sql);
421  if ($resql) {
422  while ($obj = $this->db->fetch_object($resql)) {
423  if (!empty($obj->accountancy_code)) {
424  $aux_account[$obj->accountancy_code] = $obj->accountancy_code.' <span class="opacitymedium">('.dolGetFirstLastname($obj->firstname, $obj->lastname).')</span>';
425  }
426  }
427  } else {
428  $this->error = "Error ".$this->db->lasterror();
429  dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
430  return -1;
431  }
432  $this->db->free($resql);
433 
434  // Build select
435  $out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
436 
437  return $out;
438  }
439 
440  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
450  public function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
451  {
452  // phpcs:enable
453  global $conf;
454 
455  $out_array = array();
456 
457  $sql = "SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
458  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
459  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
460  $sql .= " ORDER BY date_format(doc_date, '%Y')";
461  dol_syslog(__METHOD__, LOG_DEBUG);
462  $resql = $this->db->query($sql);
463 
464  if (!$resql) {
465  $this->error = "Error ".$this->db->lasterror();
466  dol_syslog(__METHOD__.$this->error, LOG_ERR);
467  return -1;
468  }
469  while ($obj = $this->db->fetch_object($resql)) {
470  $out_array[$obj->dtyear] = $obj->dtyear;
471  }
472  $this->db->free($resql);
473 
474  if ($output_format == 'html') {
475  return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
476  } else {
477  return $out_array;
478  }
479  }
480 }
select_auxaccount($selectid, $htmlname= 'account_num_aux', $showempty=0, $morecss= 'maxwidth200')
Return list of auxilary accounts.
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= '', $addjscombo=0, $moreparamonempty= '', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
select_accounting_category($selected= '', $htmlname= 'account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
Return list of accounting category.
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete= 'resolve')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:391
selectyear_accountancy_bookkepping($selected= '', $htmlname= 'yearid', $useempty=0, $output_format= 'html')
Return HTML combo list of years existing into book keepping.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
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.
print
Draft customers invoices.
Definition: index.php:89
Class to manage generation of HTML components for accounting management.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
select_bookkeeping_importkey($htmlname= 'importkey', $selectedkey= '')
Return select filter with date of transaction.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
__construct($db)
Constructor.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
select_account($selectid, $htmlname= 'account', $showempty=0, $event=array(), $select_in=0, $select_out=0, $morecss= 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache= '')
Return list of accounts with label by chart of accounts.