dolibarr  9.0.0
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-2017 Alexandre Spangaro <aspangaro@zendsi.com>
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 <http://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.NotCamelCaps
73  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  }
86  else
87  {
88  $sql = "SELECT rowid, code, label, nature, entity, active";
89  $sql.= " FROM " . MAIN_DB_PREFIX . "accounting_journal";
90  $sql.= " WHERE active = 1";
91  $sql.= " AND entity = ".$conf->entity;
92  //if ($nature && is_numeric($nature)) $sql .= " AND nature = ".$nature;
93  $sql.= " ORDER BY code";
94 
95  dol_syslog(get_class($this) . "::select_journal", LOG_DEBUG);
96  $resql = $this->db->query($sql);
97 
98  if (!$resql) {
99  $this->error = "Error ".$this->db->lasterror();
100  dol_syslog(get_class($this)."::select_journal ".$this->error, LOG_ERR);
101  return -1;
102  }
103 
104  $selected = 0;
105  $langs->load('accountancy');
106  while ($obj = $this->db->fetch_object($resql))
107  {
108  $label = $obj->code . ' - ' . $langs->trans($obj->label);
109 
110  $select_value_in = $obj->rowid;
111  $select_value_out = $obj->rowid;
112 
113  // Try to guess if we have found default value
114  if ($select_in == 1) {
115  $select_value_in = $obj->code;
116  }
117  if ($select_out == 1) {
118  $select_value_out = $obj->code;
119  }
120  // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
121  if ($selectid != '' && $selectid == $select_value_in) {
122  //var_dump("Found ".$selectid." ".$select_value_in);
123  $selected = $select_value_out;
124  }
125 
126  $options[$select_value_out] = $label;
127  }
128  $this->db->free($resql);
129 
130  if ($usecache)
131  {
132  $this->options_cache[$usecache] = $options;
133  }
134  }
135 
136  $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, ($disabledajaxcombo?0:1));
137 
138  return $out;
139  }
140 
141  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
154  function select_accounting_category($selected='',$htmlname='account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
155  {
156  // phpcs:enable
157  global $db,$langs,$user,$mysoc;
158 
159  if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries))
160  {
161  dol_print_error('','Call to select_accounting_account with mysoc country not yet defined');
162  exit;
163  }
164 
165  if (! empty($mysoc->country_id))
166  {
167  $sql = "SELECT c.rowid, c.label as type, c.range_account";
168  $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c";
169  $sql.= " WHERE c.active = 1";
170  $sql.= " AND c.category_type = 0";
171  if (empty($allcountries)) $sql.= " AND c.fk_country = ".$mysoc->country_id;
172  $sql.= " ORDER BY c.label ASC";
173  }
174  else
175  {
176  $sql = "SELECT c.rowid, c.label as type, c.range_account";
177  $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c, ".MAIN_DB_PREFIX."c_country as co";
178  $sql.= " WHERE c.active = 1";
179  $sql.= " AND c.category_type = 0";
180  $sql.= " AND c.fk_country = co.rowid";
181  if (empty($allcountries)) $sql.= " AND co.code = '".$mysoc->country_code."'";
182  $sql.= " ORDER BY c.label ASC";
183  }
184 
185  dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
186  $resql=$db->query($sql);
187  if ($resql)
188  {
189  $num = $db->num_rows($resql);
190  if ($num)
191  {
192  $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
193  $i = 0;
194 
195  if ($useempty) $out.= '<option value="0">&nbsp;</option>';
196  while ($i < $num)
197  {
198  $obj = $db->fetch_object($resql);
199  $out .= '<option value="'.$obj->rowid.'"';
200  if ($obj->rowid == $selected) $out .= ' selected';
201  $out .= '>'.($maxlen ? dol_trunc($obj->type,$maxlen) : $obj->type);
202  $out .= ' ('.$obj->range_account.')';
203  $i++;
204  }
205  $out .= '</select>';
206  //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
207  }
208  else
209  {
210  $out .= $langs->trans("ErrorNoAccountingCategoryForThisCountry",$mysoc->country_code);
211  }
212  }
213  else
214  {
215  dol_print_error($db,$db->lasterror());
216  }
217 
218  $out .= ajax_combobox($htmlname, array());
219 
220  print $out;
221  }
222 
223  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
231  function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '')
232  {
233  // phpcs:enable
234  $options = array();
235 
236  $sql = 'SELECT DISTINCT import_key from ' . MAIN_DB_PREFIX . 'accounting_bookkeeping';
237  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
238  $sql .= ' ORDER BY import_key DESC';
239 
240  dol_syslog(get_class($this) . "::select_bookkeeping_importkey", LOG_DEBUG);
241  $resql = $this->db->query($sql);
242 
243  if (!$resql) {
244  $this->error = "Error " . $this->db->lasterror();
245  dol_syslog(get_class($this) . "::select_bookkeeping_importkey " . $this->error, LOG_ERR);
246  return - 1;
247  }
248 
249  while ($obj = $this->db->fetch_object($resql)) {
250  $options[$obj->import_key] = dol_print_date($obj->import_key, 'dayhourtext');
251  }
252 
253  return Form::selectarray($htmlname, $options, $selectedkey);
254  }
255 
256  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
270  function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='')
271  {
272  // phpcs:enable
273  global $conf, $langs;
274 
275  require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
276 
277  $out = '';
278 
279  $options = array();
280  if ($usecache && ! empty($this->options_cache[$usecache]))
281  {
282  $options = $this->options_cache[$usecache];
283  $selected=$selectid;
284  }
285  else
286  {
287  $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT) ? 50 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT;
288 
289  $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version";
290  $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
291  $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
292  $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
293  $sql .= " AND aa.active = 1";
294  $sql .= " AND aa.entity=".$conf->entity;
295  $sql .= " ORDER BY aa.account_number";
296 
297  dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
298  $resql = $this->db->query($sql);
299 
300  if (!$resql) {
301  $this->error = "Error " . $this->db->lasterror();
302  dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
303  return -1;
304  }
305 
306  $selected = 0;
307  while ($obj = $this->db->fetch_object($resql))
308  {
309  $label = length_accountg($obj->account_number) . ' - ' . $obj->label;
310  $label = dol_trunc($label, $trunclength);
311 
312  $select_value_in = $obj->rowid;
313  $select_value_out = $obj->rowid;
314 
315  // Try to guess if we have found default value
316  if ($select_in == 1) {
317  $select_value_in = $obj->account_number;
318  }
319  if ($select_out == 1) {
320  $select_value_out = $obj->account_number;
321  }
322  // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
323  // Because same account_number can be share between different accounting_system and do have the same meaning
324  if ($selectid != '' && $selectid == $select_value_in) {
325  //var_dump("Found ".$selectid." ".$select_value_in);
326  $selected = $select_value_out;
327  }
328 
329  $options[$select_value_out] = $label;
330  }
331  $this->db->free($resql);
332 
333  if ($usecache)
334  {
335  $this->options_cache[$usecache] = $options;
336  }
337  }
338 
339  if ($showempty == 2)
340  {
341  $options['0'] = $langs->trans("None");
342  }
343 
344  $out .= Form::selectarray($htmlname, $options, $selected, ($showempty > 0 ? 1 : 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
345 
346  return $out;
347  }
348 
349  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
359  function select_auxaccount($selectid, $htmlname='account_num_aux', $showempty=0, $morecss='maxwidth200')
360  {
361  // phpcs:enable
362 
363  $aux_account = array();
364 
365  // Auxiliary customer account
366  $sql = "SELECT DISTINCT code_compta, nom ";
367  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
368  $sql .= " WHERE entity IN (" . getEntity('societe') . ")";
369  $sql .= " ORDER BY code_compta";
370  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
371  $resql = $this->db->query($sql);
372  if ($resql) {
373  while ($obj = $this->db->fetch_object($resql)) {
374  if (!empty($obj->code_compta)) {
375  $aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')';
376  }
377  }
378  } else {
379  $this->error = "Error ".$this->db->lasterror();
380  dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
381  return -1;
382  }
383  $this->db->free($resql);
384 
385  // Auxiliary supplier account
386  $sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
387  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
388  $sql .= " WHERE entity IN (" . getEntity('societe') . ")";
389  $sql .= " ORDER BY code_compta_fournisseur";
390  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
391  $resql = $this->db->query($sql);
392  if ($resql) {
393  while ($obj = $this->db->fetch_object($resql)) {
394  if (!empty($obj->code_compta_fournisseur)) {
395  $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')';
396  }
397  }
398  } else {
399  $this->error = "Error ".$this->db->lasterror();
400  dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
401  return -1;
402  }
403  $this->db->free($resql);
404 
405  // Build select
406  $out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
407 
408  return $out;
409  }
410 
411  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
421  function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
422  {
423  // phpcs:enable
424  global $conf;
425 
426  $out_array = array();
427 
428  $sql = "SELECT DISTINCT date_format(doc_date,'%Y') as dtyear";
429  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
430  $sql .= " WHERE entity IN (" . getEntity('accountancy') . ")";
431  $sql .= " ORDER BY date_format(doc_date,'%Y')";
432  dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
433  $resql = $this->db->query($sql);
434 
435  if (!$resql) {
436  $this->error = "Error ".$this->db->lasterror();
437  dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
438  return -1;
439  }
440  while ($obj = $this->db->fetch_object($resql)) {
441  $out_array[$obj->dtyear] = $obj->dtyear;
442  }
443  $this->db->free($resql);
444 
445  if ($output_format == 'html') {
446  return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
447  } else {
448  return $out_array;
449  }
450  }
451 }
select_auxaccount($selectid, $htmlname='account_num_aux', $showempty=0, $morecss='maxwidth200')
Return list of auxilary thirdparty accounts.
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:374
print
Draft customers invoices.
Definition: index.php:91
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.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
selectyear_accountancy_bookkepping($selected='', $htmlname='yearid', $useempty=0, $output_format='html')
Return HTML combo list of years existing into book keepping.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
select_accounting_category($selected='', $htmlname='account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
Return list of accounting category.
select_account($selectid, $htmlname='account', $showempty=0, $event=array(), $select_in=0, $select_out=0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='')
Return list of accounts with label by chart of 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.
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
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.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to manage generation of HTML components for accounting management.
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.