dolibarr  7.0.0-beta
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 
32 class FormAccounting extends Form
33 {
34 
35  private $options_cache = array();
36 
37  var $db;
38  var $error;
39 
45  public function __construct($db)
46  {
47  $this->db = $db;
48  }
49 
64  function select_journal($selectid, $htmlname = 'journal', $nature=0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='', $disabledajaxcombo=0)
65  {
66  global $conf;
67 
68  $out = '';
69 
70  $options = array();
71  if ($usecache && ! empty($this->options_cache[$usecache]))
72  {
73  $options = $this->options_cache[$usecache];
74  $selected=$selectid;
75  }
76  else
77  {
78  $sql = "SELECT rowid, code, label, nature, entity, active";
79  $sql.= " FROM " . MAIN_DB_PREFIX . "accounting_journal";
80  $sql.= " WHERE active = 1";
81  $sql.= " AND entity = ".$conf->entity;
82  //if ($nature && is_numeric($nature)) $sql .= " AND nature = ".$nature;
83  $sql.= " ORDER BY code";
84 
85  dol_syslog(get_class($this) . "::select_journal", LOG_DEBUG);
86  $resql = $this->db->query($sql);
87 
88  if (!$resql) {
89  $this->error = "Error ".$this->db->lasterror();
90  dol_syslog(get_class($this)."::select_journal ".$this->error, LOG_ERR);
91  return -1;
92  }
93 
94  $selected = 0;
95  while ($obj = $this->db->fetch_object($resql))
96  {
97  $label = $obj->code . ' - ' . $obj->label;
98 
99  $select_value_in = $obj->rowid;
100  $select_value_out = $obj->rowid;
101 
102  // Try to guess if we have found default value
103  if ($select_in == 1) {
104  $select_value_in = $obj->code;
105  }
106  if ($select_out == 1) {
107  $select_value_out = $obj->code;
108  }
109  // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
110  if ($selectid != '' && $selectid == $select_value_in) {
111  //var_dump("Found ".$selectid." ".$select_value_in);
112  $selected = $select_value_out;
113  }
114 
115  $options[$select_value_out] = $label;
116  }
117  $this->db->free($resql);
118 
119  if ($usecache)
120  {
121  $this->options_cache[$usecache] = $options;
122  }
123  }
124 
125  $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, ($disabledajaxcombo?0:1));
126 
127  return $out;
128  }
129 
142  function select_accounting_category($selected='',$htmlname='account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
143  {
144  global $db,$langs,$user,$mysoc;
145 
146  if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries))
147  {
148  dol_print_error('','Call to select_accounting_account with mysoc country not yet defined');
149  exit;
150  }
151 
152  if (! empty($mysoc->country_id))
153  {
154  $sql = "SELECT c.rowid, c.label as type, c.range_account";
155  $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c";
156  $sql.= " WHERE c.active = 1";
157  $sql.= " AND c.category_type = 0";
158  if (empty($allcountries)) $sql.= " AND c.fk_country = ".$mysoc->country_id;
159  $sql.= " ORDER BY c.label ASC";
160  }
161  else
162  {
163  $sql = "SELECT c.rowid, c.label as type, c.range_account";
164  $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c, ".MAIN_DB_PREFIX."c_country as co";
165  $sql.= " WHERE c.active = 1";
166  $sql.= " AND c.category_type = 0";
167  $sql.= " AND c.fk_country = co.rowid";
168  if (empty($allcountries)) $sql.= " AND co.code = '".$mysoc->country_code."'";
169  $sql.= " ORDER BY c.label ASC";
170  }
171 
172  dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
173  $resql=$db->query($sql);
174  if ($resql)
175  {
176  $num = $db->num_rows($resql);
177  if ($num)
178  {
179  $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
180  $i = 0;
181 
182  if ($useempty) $out.= '<option value="0">&nbsp;</option>';
183  while ($i < $num)
184  {
185  $obj = $db->fetch_object($resql);
186  $out .= '<option value="'.$obj->rowid.'"';
187  if ($obj->rowid == $selected) $out .= ' selected';
188  $out .= '>'.($maxlen ? dol_trunc($obj->type,$maxlen) : $obj->type);
189  $out .= ' ('.$obj->range_account.')';
190  $i++;
191  }
192  $out .= '</select>';
193  //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
194  }
195  else
196  {
197  $out .= $langs->trans("ErrorNoAccountingCategoryForThisCountry",$mysoc->country_code);
198  }
199  }
200  else
201  {
202  dol_print_error($db,$db->lasterror());
203  }
204 
205  $out .= ajax_combobox($htmlname, array());
206 
207  print $out;
208  }
209 
217  function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '') {
218  $options = array();
219 
220  $sql = 'SELECT DISTINCT import_key from ' . MAIN_DB_PREFIX . 'accounting_bookkeeping';
221  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
222  $sql .= ' ORDER BY import_key DESC';
223 
224  dol_syslog(get_class($this) . "::select_bookkeeping_importkey", LOG_DEBUG);
225  $resql = $this->db->query($sql);
226 
227  if (!$resql) {
228  $this->error = "Error " . $this->db->lasterror();
229  dol_syslog(get_class($this) . "::select_bookkeeping_importkey " . $this->error, LOG_ERR);
230  return - 1;
231  }
232 
233  while ($obj = $this->db->fetch_object($resql)) {
234  $options[$obj->import_key] = dol_print_date($obj->import_key, 'dayhourtext');
235  }
236 
237  return Form::selectarray($htmlname, $options, $selectedkey);
238  }
239 
253  function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='')
254  {
255  global $conf;
256 
257  require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
258 
259  $out = '';
260 
261  $options = array();
262  if ($usecache && ! empty($this->options_cache[$usecache]))
263  {
264  $options = $this->options_cache[$usecache];
265  $selected=$selectid;
266  }
267  else
268  {
269  $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT) ? 50 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT;
270 
271  $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version";
272  $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
273  $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
274  $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
275  $sql .= " AND aa.active = 1";
276  $sql .= " ORDER BY aa.account_number";
277 
278  dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
279  $resql = $this->db->query($sql);
280 
281  if (!$resql) {
282  $this->error = "Error " . $this->db->lasterror();
283  dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
284  return -1;
285  }
286 
287  $selected = 0;
288  while ($obj = $this->db->fetch_object($resql))
289  {
290  $label = length_accountg($obj->account_number) . ' - ' . $obj->label;
291  $label = dol_trunc($label, $trunclength);
292 
293  $select_value_in = $obj->rowid;
294  $select_value_out = $obj->rowid;
295 
296  // Try to guess if we have found default value
297  if ($select_in == 1) {
298  $select_value_in = $obj->account_number;
299  }
300  if ($select_out == 1) {
301  $select_value_out = $obj->account_number;
302  }
303  // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
304  // Because same account_number can be share between different accounting_system and do have the same meaning
305  if ($selectid != '' && $selectid == $select_value_in) {
306  //var_dump("Found ".$selectid." ".$select_value_in);
307  $selected = $select_value_out;
308  }
309 
310  $options[$select_value_out] = $label;
311  }
312  $this->db->free($resql);
313 
314  if ($usecache)
315  {
316  $this->options_cache[$usecache] = $options;
317  }
318  }
319 
320  $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
321 
322  return $out;
323  }
324 
334  function select_auxaccount($selectid, $htmlname='account_num_aux', $showempty=0, $morecss='maxwidth200') {
335 
336  $aux_account = array();
337 
338  // Auxiliary customer account
339  $sql = "SELECT DISTINCT code_compta, nom ";
340  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
341  $sql .= " WHERE entity IN (" . getEntity('societe') . ")";
342  $sql .= " ORDER BY code_compta";
343  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
344  $resql = $this->db->query($sql);
345  if ($resql) {
346  while ($obj = $this->db->fetch_object($resql)) {
347  if (!empty($obj->code_compta)) {
348  $aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')';
349  }
350  }
351  } else {
352  $this->error = "Error ".$this->db->lasterror();
353  dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
354  return -1;
355  }
356  $this->db->free($resql);
357 
358  // Auxiliary supplier account
359  $sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
360  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
361  $sql .= " WHERE entity IN (" . getEntity('societe') . ")";
362  $sql .= " ORDER BY code_compta_fournisseur";
363  dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
364  $resql = $this->db->query($sql);
365  if ($resql) {
366  while ($obj = $this->db->fetch_object($resql)) {
367  if (!empty($obj->code_compta_fournisseur)) {
368  $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')';
369  }
370  }
371  } else {
372  $this->error = "Error ".$this->db->lasterror();
373  dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
374  return -1;
375  }
376  $this->db->free($resql);
377 
378  // Build select
379  $out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
380 
381  return $out;
382  }
383 
393  function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
394  {
395  global $conf;
396 
397  $out_array = array();
398 
399  $sql = "SELECT DISTINCT date_format(doc_date,'%Y') as dtyear";
400  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
401  $sql .= " WHERE entity IN (" . getEntity('accountancy') . ")";
402  $sql .= " ORDER BY date_format(doc_date,'%Y')";
403  dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
404  $resql = $this->db->query($sql);
405 
406  if (!$resql) {
407  $this->error = "Error ".$this->db->lasterror();
408  dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
409  return -1;
410  }
411  while ($obj = $this->db->fetch_object($resql)) {
412  $out_array[$obj->dtyear] = $obj->dtyear;
413  }
414  $this->db->free($resql);
415 
416  if ($output_format == 'html') {
417  return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
418  } else {
419  return $out_array;
420  }
421  }
422 }
423 
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:392
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_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
selectyear_accountancy_bookkepping($selected= '', $htmlname= 'yearid', $useempty=0, $output_format= 'html')
Return HTML combo list of years existing into book keepping.
select_accounting_category($selected='', $htmlname='account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
Return list of accounting category.
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.
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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
Class to manage generation of HTML components for accounting management.
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:1013
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.
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.