dolibarr 19.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
27require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
28
29
33class FormAccounting extends Form
34{
35 private $options_cache = array();
36
40 public $db;
41
45 public $error = '';
46
50 public $nbaccounts;
54 public $nbaccounts_category;
55
56
62 public function __construct($db)
63 {
64 $this->db = $db;
65 }
66
67 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
82 public function select_journal($selectid, $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = 'maxwidth300 maxwidthonsmartphone', $usecache = '', $disabledajaxcombo = 0)
83 {
84 // phpcs:enable
85 global $conf, $langs;
86
87 $out = '';
88
89 $options = array();
90 if ($usecache && !empty($this->options_cache[$usecache])) {
91 $options = $this->options_cache[$usecache];
92 $selected = $selectid;
93 } else {
94 $sql = "SELECT rowid, code, label, nature, entity, active";
95 $sql .= " FROM ".$this->db->prefix()."accounting_journal";
96 $sql .= " WHERE active = 1";
97 $sql .= " AND entity = ".$conf->entity;
98 if ($nature && is_numeric($nature)) {
99 $sql .= " AND nature = ".((int) $nature);
100 }
101 $sql .= " ORDER BY code";
102
103 dol_syslog(get_class($this)."::select_journal", LOG_DEBUG);
104 $resql = $this->db->query($sql);
105
106 if (!$resql) {
107 $this->error = "Error ".$this->db->lasterror();
108 dol_syslog(get_class($this)."::select_journal ".$this->error, LOG_ERR);
109 return -1;
110 }
111
112 $selected = 0;
113 $langs->load('accountancy');
114 while ($obj = $this->db->fetch_object($resql)) {
115 $label = $obj->code.' - '.$langs->trans($obj->label);
116
117 $select_value_in = $obj->rowid;
118 $select_value_out = $obj->rowid;
119
120 // Try to guess if we have found default value
121 if ($select_in == 1) {
122 $select_value_in = $obj->code;
123 }
124 if ($select_out == 1) {
125 $select_value_out = $obj->code;
126 }
127 // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
128 if ($selectid != '' && $selectid == $select_value_in) {
129 //var_dump("Found ".$selectid." ".$select_value_in);
130 $selected = $select_value_out;
131 }
132
133 $options[$select_value_out] = $label;
134 }
135 $this->db->free($resql);
136
137 if ($usecache) {
138 $this->options_cache[$usecache] = $options;
139 }
140 }
141
142 $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, ($disabledajaxcombo ? 0 : 1));
143
144 return $out;
145 }
146
147 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
162 public function multi_select_journal($selectedIds = array(), $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = '', $usecache = '', $disabledajaxcombo = 0)
163 {
164 // phpcs:enable
165 global $conf, $langs;
166
167 $out = '';
168
169 $options = array();
170 if ($usecache && !empty($this->options_cache[$usecache])) {
171 $options = $this->options_cache[$usecache];
172 $selected = $selectedIds;
173 } else {
174 $sql = "SELECT rowid, code, label, nature, entity, active";
175 $sql .= " FROM ".$this->db->prefix()."accounting_journal";
176 $sql .= " WHERE active = 1";
177 $sql .= " AND entity = ".$conf->entity;
178 if ($nature && is_numeric($nature)) {
179 $sql .= " AND nature = ".((int) $nature);
180 }
181 $sql .= " ORDER BY code";
182
183 dol_syslog(get_class($this)."::multi_select_journal", LOG_DEBUG);
184 $resql = $this->db->query($sql);
185
186 if (!$resql) {
187 $this->error = "Error ".$this->db->lasterror();
188 dol_syslog(get_class($this)."::multi_select_journal ".$this->error, LOG_ERR);
189 return -1;
190 }
191
192 $selected = array();
193 $langs->load('accountancy');
194 while ($obj = $this->db->fetch_object($resql)) {
195 $label = $langs->trans($obj->label);
196
197 $select_value_in = $obj->rowid;
198 $select_value_out = $obj->rowid;
199
200 // Try to guess if we have found default value
201 if ($select_in == 1) {
202 $select_value_in = $obj->code;
203 }
204 if ($select_out == 1) {
205 $select_value_out = $obj->code;
206 }
207 // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
208 if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
209 //var_dump("Found ".$selectid." ".$select_value_in);
210 $selected[] = $select_value_out;
211 }
212 $options[$select_value_out] = $label;
213 }
214 $this->db->free($resql);
215
216 if ($usecache) {
217 $this->options_cache[$usecache] = $options;
218 }
219 }
220
221 $out .= Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0, 'code_journal', '', ($disabledajaxcombo ? 0 : 1));
222
223 return $out;
224 }
225
226 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
239 public function select_accounting_category($selected = '', $htmlname = 'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
240 {
241 // phpcs:enable
242 global $langs, $mysoc;
243
244 if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) {
245 dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
246 exit;
247 }
248
249 if (!empty($mysoc->country_id)) {
250 $sql = "SELECT c.rowid, c.label as type, c.range_account";
251 $sql .= " FROM ".$this->db->prefix()."c_accounting_category as c";
252 $sql .= " WHERE c.active = 1";
253 $sql .= " AND c.category_type = 0";
254 if (empty($allcountries)) {
255 $sql .= " AND c.fk_country = ".((int) $mysoc->country_id);
256 }
257 $sql .= " ORDER BY c.label ASC";
258 } else {
259 $sql = "SELECT c.rowid, c.label as type, c.range_account";
260 $sql .= " FROM ".$this->db->prefix()."c_accounting_category as c, ".$this->db->prefix()."c_country as co";
261 $sql .= " WHERE c.active = 1";
262 $sql .= " AND c.category_type = 0";
263 $sql .= " AND c.fk_country = co.rowid";
264 if (empty($allcountries)) {
265 $sql .= " AND co.code = '".$this->db->escape($mysoc->country_code)."'";
266 }
267 $sql .= " ORDER BY c.label ASC";
268 }
269
270 $this->nbaccounts_category = 0;
271
272 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
273 $resql = $this->db->query($sql);
274 if ($resql) {
275 $num = $this->db->num_rows($resql);
276 if ($num) {
277 $this->nbaccounts_category = $num;
278
279 $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
280 $i = 0;
281
282 if ($useempty) {
283 $out .= '<option value="0">&nbsp;</option>';
284 }
285 while ($i < $num) {
286 $obj = $this->db->fetch_object($resql);
287
288 $titletoshowhtml = ($maxlen ? dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ? ' <span class="opacitymedium">('.$obj->range_account.')</span>' : '');
289 $titletoshow = ($maxlen ? dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ? ' ('.$obj->range_account.')' : '');
290
291 $out .= '<option value="'.$obj->rowid.'"';
292 if ($obj->rowid == $selected) {
293 $out .= ' selected';
294 }
295 //$out .= ' data-html="'.dol_escape_htmltag(dol_string_onlythesehtmltags($titletoshowhtml, 1, 0, 0, 0, array('span'))).'"';
296 $out .= ' data-html="'.dolPrintHTMLForAttribute($titletoshowhtml).'"';
297 $out .= '>';
298 $out .= dol_escape_htmltag($titletoshow);
299 $out .= '</option>';
300 $i++;
301 }
302 $out .= '</select>';
303 //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
304 } else {
305 $out = $langs->trans("ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->transnoentitiesnoconv("Accounting"), $langs->transnoentitiesnoconv("Setup"), $langs->transnoentitiesnoconv("AccountingCategories"));
306 }
307 } else {
308 dol_print_error($this->db);
309 }
310
311 $out .= ajax_combobox($htmlname, array());
312
313 return $out;
314 }
315
316 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
324 public function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '')
325 {
326 // phpcs:enable
327 $options = array();
328
329 $sql = "SELECT DISTINCT import_key FROM ".$this->db->prefix()."accounting_bookkeeping";
330 $sql .= " WHERE entity IN (".getEntity('accountancy').")";
331 $sql .= ' ORDER BY import_key DESC';
332
333 dol_syslog(get_class($this)."::select_bookkeeping_importkey", LOG_DEBUG);
334 $resql = $this->db->query($sql);
335
336 if (!$resql) {
337 $this->error = "Error ".$this->db->lasterror();
338 dol_syslog(get_class($this)."::select_bookkeeping_importkey ".$this->error, LOG_ERR);
339 return -1;
340 }
341
342 while ($obj = $this->db->fetch_object($resql)) {
343 $options[$obj->import_key] = $obj->import_key;
344 }
345
346 return Form::selectarray($htmlname, $options, $selectedkey);
347 }
348
349 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
364 public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss = 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache = '', $active = '1')
365 {
366 // phpcs:enable
367 global $conf, $langs;
368
369 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
370
371 $out = '';
372
373 $options = array();
374
375 if ($showempty == 2) {
376 $options['0'] = '--- '.$langs->trans("None").' ---';
377 }
378
379 if ($usecache && !empty($this->options_cache[$usecache])) {
380 $options = $options + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
381 $selected = $selectid;
382 } else {
383 $trunclength = getDolGlobalInt('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
384
385 $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
386 $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
387 $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
388 $sql .= " AND asy.rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
389 if ($active === '1') {
390 $sql .= " AND aa.active = 1";
391 } elseif ($active === '0') {
392 $sql .= " AND aa.active = 0";
393 }
394 $sql .= " AND aa.entity=".((int) $conf->entity);
395 $sql .= " ORDER BY aa.account_number";
396
397 dol_syslog(get_class($this)."::select_account", LOG_DEBUG);
398 $resql = $this->db->query($sql);
399
400 if (!$resql) {
401 $this->error = "Error ".$this->db->lasterror();
402 dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR);
403 return -1;
404 }
405
406 $num_rows = $this->db->num_rows($resql);
407
408 if ($num_rows == 0 && getDolGlobalInt('CHARTOFACCOUNTS') <= 0) {
409 $langs->load("errors");
410 $showempty = $langs->trans("ErrorYouMustFirstSetupYourChartOfAccount");
411 } else {
412 $selected = $selectid; // selectid can be -1, 0, 123
413 while ($obj = $this->db->fetch_object($resql)) {
414 if (empty($obj->labelshort)) {
415 $labeltoshow = $obj->label;
416 } else {
417 $labeltoshow = $obj->labelshort;
418 }
419
420 $label = length_accountg($obj->account_number).' - '.$labeltoshow;
421 $label = dol_trunc($label, $trunclength);
422
423 $select_value_in = $obj->rowid;
424 $select_value_out = $obj->rowid;
425
426 // Try to guess if we have found default value
427 if ($select_in == 1) {
428 $select_value_in = $obj->account_number;
429 }
430 if ($select_out == 1) {
431 $select_value_out = $obj->account_number;
432 }
433 // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
434 // Because same account_number can be share between different accounting_system and do have the same meaning
435 if ($selectid != '' && $selectid == $select_value_in) {
436 //var_dump("Found ".$selectid." ".$select_value_in);
437 $selected = $select_value_out;
438 }
439
440 $options[$select_value_out] = $label;
441 }
442 }
443
444 $this->db->free($resql);
445
446 if ($usecache) {
447 $this->options_cache[$usecache] = $options;
448 unset($this->options_cache[$usecache]['0']);
449 }
450 }
451
452
453 $out .= Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
454
455 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
456
457 return $out;
458 }
459
460 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
472 public function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $morecss = 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache = '', $labelhtmlname = '')
473 {
474 // phpcs:enable
475 global $conf;
476
477 $aux_account = array();
478
479 if ($usecache && !empty($this->options_cache[$usecache])) {
480 $aux_account = $aux_account + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
481 } else {
482 dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
483
484 // Auxiliary thirdparties account
485 $sql = "SELECT code_compta, code_compta_fournisseur, nom as name";
486 $sql .= " FROM ".$this->db->prefix()."societe";
487 $sql .= " WHERE entity IN (".getEntity('societe').")";
488 $sql .= " AND (client IN (1,3) OR fournisseur = 1)";
489
490 $resql = $this->db->query($sql);
491 if ($resql) {
492 while ($obj = $this->db->fetch_object($resql)) {
493 if (!empty($obj->code_compta)) {
494 $aux_account[$obj->code_compta] = $obj->code_compta.' <span class="opacitymedium">('.$obj->name.')</span>';
495 }
496 if (!empty($obj->code_compta_fournisseur)) {
497 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' <span class="opacitymedium">('.$obj->name.')</span>';
498 }
499 }
500 } else {
501 $this->error = "Error ".$this->db->lasterror();
502 dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
503 return -1;
504 }
505
506 ksort($aux_account);
507
508 $this->db->free($resql);
509
510 // Auxiliary user account
511 $sql = "SELECT DISTINCT accountancy_code, lastname, firstname ";
512 $sql .= " FROM ".$this->db->prefix()."user";
513 $sql .= " WHERE entity IN (".getEntity('user').")";
514 $sql .= " ORDER BY accountancy_code";
515
516 $resql = $this->db->query($sql);
517 if ($resql) {
518 while ($obj = $this->db->fetch_object($resql)) {
519 if (!empty($obj->accountancy_code)) {
520 $aux_account[$obj->accountancy_code] = $obj->accountancy_code.' <span class="opacitymedium">('.dolGetFirstLastname($obj->firstname, $obj->lastname).')</span>';
521 }
522 }
523 } else {
524 $this->error = "Error ".$this->db->lasterror();
525 dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
526 return -1;
527 }
528 $this->db->free($resql);
529
530 if ($usecache) {
531 $this->options_cache[$usecache] = $aux_account;
532 }
533 }
534
535 // Build select
536 $out = '';
537 $out .= Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
538 //automatic filling if we give the name of the subledger_label input
539 if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
540 $out .= '<script nonce="'.getNonce().'">
541 jQuery(document).ready(() => {
542 $("#'.$htmlname.'").on("select2:select", function(e) {
543 var regExp = /\‍(([^)]+)\‍)/;
544 const match = regExp.exec(e.params.data.text);
545 $(\'input[name="'.dol_escape_js($labelhtmlname).'"]\').val(match[1]);
546 });
547 });
548
549 </script>';
550 }
551
552 return $out;
553 }
554
555 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
565 public function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
566 {
567 // phpcs:enable
568 global $conf;
569
570 $out_array = array();
571
572 $sql = "SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
573 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
574 $sql .= " WHERE entity IN (".getEntity('accountancy').")";
575 $sql .= " ORDER BY date_format(doc_date, '%Y')";
576 dol_syslog(__METHOD__, LOG_DEBUG);
577 $resql = $this->db->query($sql);
578
579 if (!$resql) {
580 $this->error = "Error ".$this->db->lasterror();
581 dol_syslog(__METHOD__.$this->error, LOG_ERR);
582 return -1;
583 }
584 while ($obj = $this->db->fetch_object($resql)) {
585 $out_array[$obj->dtyear] = $obj->dtyear;
586 }
587 $this->db->free($resql);
588
589 if ($output_format == 'html') {
590 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
591 } else {
592 return $out_array;
593 }
594 }
595}
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:455
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='minwidth100 maxwidth300 maxwidthonsmartphone', $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.
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 a Dolibarr global constant int value.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...