dolibarr 18.0.6
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, 1, 0, 0, array('span'))).'"';
296 $out .= '>';
297 $out .= dol_escape_htmltag($titletoshow);
298 $out .= '</option>';
299 $i++;
300 }
301 $out .= '</select>';
302 //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
303 } else {
304 $out = $langs->trans("ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->trans("Accounting"), $langs->trans("Setup"), $langs->trans("AccountingCategories"));
305 }
306 } else {
307 dol_print_error($this->db);
308 }
309
310 $out .= ajax_combobox($htmlname, array());
311
312 return $out;
313 }
314
315 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
323 public function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '')
324 {
325 // phpcs:enable
326 $options = array();
327
328 $sql = "SELECT DISTINCT import_key FROM ".$this->db->prefix()."accounting_bookkeeping";
329 $sql .= " WHERE entity IN (".getEntity('accountancy').")";
330 $sql .= ' ORDER BY import_key DESC';
331
332 dol_syslog(get_class($this)."::select_bookkeeping_importkey", LOG_DEBUG);
333 $resql = $this->db->query($sql);
334
335 if (!$resql) {
336 $this->error = "Error ".$this->db->lasterror();
337 dol_syslog(get_class($this)."::select_bookkeeping_importkey ".$this->error, LOG_ERR);
338 return -1;
339 }
340
341 while ($obj = $this->db->fetch_object($resql)) {
342 $options[$obj->import_key] = $obj->import_key;
343 }
344
345 return Form::selectarray($htmlname, $options, $selectedkey);
346 }
347
348 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
363 public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss = 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache = '', $active = '1')
364 {
365 // phpcs:enable
366 global $conf, $langs;
367
368 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
369
370 $out = '';
371
372 $options = array();
373
374 if ($showempty == 2) {
375 $options['0'] = '--- '.$langs->trans("None").' ---';
376 }
377
378 if ($usecache && !empty($this->options_cache[$usecache])) {
379 $options = $options + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
380 $selected = $selectid;
381 } else {
382 $trunclength = getDolGlobalInt('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
383
384 $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
385 $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
386 $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
387 $sql .= " AND asy.rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
388 if ($active === '1') {
389 $sql .= " AND aa.active = 1";
390 } elseif ($active === '0') {
391 $sql .= " AND aa.active = 0";
392 }
393 $sql .= " AND aa.entity=".((int) $conf->entity);
394 $sql .= " ORDER BY aa.account_number";
395
396 dol_syslog(get_class($this)."::select_account", LOG_DEBUG);
397 $resql = $this->db->query($sql);
398
399 if (!$resql) {
400 $this->error = "Error ".$this->db->lasterror();
401 dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR);
402 return -1;
403 }
404
405 $num_rows = $this->db->num_rows($resql);
406
407 if ($num_rows == 0 && getDolGlobalInt('CHARTOFACCOUNTS') <= 0) {
408 $langs->load("errors");
409 $showempty = $langs->trans("ErrorYouMustFirstSetupYourChartOfAccount");
410 } else {
411 $selected = $selectid; // selectid can be -1, 0, 123
412 while ($obj = $this->db->fetch_object($resql)) {
413 if (empty($obj->labelshort)) {
414 $labeltoshow = $obj->label;
415 } else {
416 $labeltoshow = $obj->labelshort;
417 }
418
419 $label = length_accountg($obj->account_number).' - '.$labeltoshow;
420 $label = dol_trunc($label, $trunclength);
421
422 $select_value_in = $obj->rowid;
423 $select_value_out = $obj->rowid;
424
425 // Try to guess if we have found default value
426 if ($select_in == 1) {
427 $select_value_in = $obj->account_number;
428 }
429 if ($select_out == 1) {
430 $select_value_out = $obj->account_number;
431 }
432 // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
433 // Because same account_number can be share between different accounting_system and do have the same meaning
434 if ($selectid != '' && $selectid == $select_value_in) {
435 //var_dump("Found ".$selectid." ".$select_value_in);
436 $selected = $select_value_out;
437 }
438
439 $options[$select_value_out] = $label;
440 }
441 }
442
443 $this->db->free($resql);
444
445 if ($usecache) {
446 $this->options_cache[$usecache] = $options;
447 unset($this->options_cache[$usecache]['0']);
448 }
449 }
450
451
452 $out .= Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
453
454 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
455
456 return $out;
457 }
458
459 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
471 public function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $morecss = 'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache = '', $labelhtmlname = '')
472 {
473 // phpcs:enable
474 global $conf;
475
476 $aux_account = array();
477
478 if ($usecache && !empty($this->options_cache[$usecache])) {
479 $aux_account = $aux_account + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0
480 } else {
481 dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
482
483 // Auxiliary thirdparties account
484 $sql = "SELECT code_compta, code_compta_fournisseur, nom as name";
485 $sql .= " FROM ".$this->db->prefix()."societe";
486 $sql .= " WHERE entity IN (".getEntity('societe').")";
487 $sql .= " AND (client IN (1,3) OR fournisseur = 1)";
488
489 $resql = $this->db->query($sql);
490 if ($resql) {
491 while ($obj = $this->db->fetch_object($resql)) {
492 if (!empty($obj->code_compta)) {
493 $aux_account[$obj->code_compta] = $obj->code_compta.' <span class="opacitymedium">('.$obj->name.')</span>';
494 }
495 if (!empty($obj->code_compta_fournisseur)) {
496 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' <span class="opacitymedium">('.$obj->name.')</span>';
497 }
498 }
499 } else {
500 $this->error = "Error ".$this->db->lasterror();
501 dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
502 return -1;
503 }
504
505 ksort($aux_account);
506
507 $this->db->free($resql);
508
509 // Auxiliary user account
510 $sql = "SELECT DISTINCT accountancy_code, lastname, firstname ";
511 $sql .= " FROM ".$this->db->prefix()."user";
512 $sql .= " WHERE entity IN (".getEntity('user').")";
513 $sql .= " ORDER BY accountancy_code";
514
515 $resql = $this->db->query($sql);
516 if ($resql) {
517 while ($obj = $this->db->fetch_object($resql)) {
518 if (!empty($obj->accountancy_code)) {
519 $aux_account[$obj->accountancy_code] = $obj->accountancy_code.' <span class="opacitymedium">('.dolGetFirstLastname($obj->firstname, $obj->lastname).')</span>';
520 }
521 }
522 } else {
523 $this->error = "Error ".$this->db->lasterror();
524 dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR);
525 return -1;
526 }
527 $this->db->free($resql);
528
529 if ($usecache) {
530 $this->options_cache[$usecache] = $aux_account;
531 }
532 }
533
534 // Build select
535 $out = '';
536 $out .= Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0, '', 0, 0, 0, '', $morecss, 1);
537 //automatic filling if we give the name of the subledger_label input
538 if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
539 $out .= '<script nonce="'.getNonce().'">
540 jQuery(document).ready(() => {
541 $("#'.$htmlname.'").on("select2:select", function(e) {
542 var regExp = /\‍(([^)]+)\‍)/;
543 const match = regExp.exec(e.params.data.text);
544 $(\'input[name="'.dol_escape_js($labelhtmlname).'"]\').val(match[1]);
545 });
546 });
547
548 </script>';
549 }
550
551 return $out;
552 }
553
554 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
564 public function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
565 {
566 // phpcs:enable
567 global $conf;
568
569 $out_array = array();
570
571 $sql = "SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
572 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
573 $sql .= " WHERE entity IN (".getEntity('accountancy').")";
574 $sql .= " ORDER BY date_format(doc_date, '%Y')";
575 dol_syslog(__METHOD__, LOG_DEBUG);
576 $resql = $this->db->query($sql);
577
578 if (!$resql) {
579 $this->error = "Error ".$this->db->lasterror();
580 dol_syslog(__METHOD__.$this->error, LOG_ERR);
581 return -1;
582 }
583 while ($obj = $this->db->fetch_object($resql)) {
584 $out_array[$obj->dtyear] = $obj->dtyear;
585 }
586 $this->db->free($resql);
587
588 if ($output_format == 'html') {
589 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
590 } else {
591 return $out_array;
592 }
593 }
594}
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:464
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 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(), $allowlink=0)
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.
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...