29require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
40 private $options_cache = array();
59 public $nbaccounts_category;
87 public function select_journal($selectid, $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'maxwidth300 maxwidthonsmartphone', $usecache =
'', $disabledajaxcombo = 0)
95 if ($usecache && !empty($this->options_cache[$usecache])) {
96 $options = $this->options_cache[$usecache];
97 $selected = $selectid;
99 $sql =
"SELECT rowid, code, label, nature, entity, active";
100 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
101 $sql .=
" WHERE active = 1";
102 $sql .=
" AND entity = ".((int) $conf->entity);
103 if ($nature && is_numeric($nature)) {
104 $sql .=
" AND nature = ".((int) $nature);
106 $sql .=
" ORDER BY code";
108 dol_syslog(get_class($this).
"::select_journal", LOG_DEBUG);
109 $resql = $this->db->query($sql);
112 $this->error =
"Error ".$this->db->lasterror();
113 dol_syslog(get_class($this).
"::select_journal ".$this->error, LOG_ERR);
118 $langs->load(
'accountancy');
119 while ($obj = $this->db->fetch_object($resql)) {
120 $label = $obj->code.
' - '.$langs->trans($obj->label);
122 $select_value_in = $obj->rowid;
123 $select_value_out = $obj->rowid;
126 if ($select_in == 1) {
127 $select_value_in = $obj->code;
129 if ($select_out == 1) {
130 $select_value_out = $obj->code;
133 if ($selectid !=
'' && $selectid == $select_value_in) {
135 $selected = $select_value_out;
138 $options[$select_value_out] = $label;
140 $this->db->free($resql);
143 $this->options_cache[$usecache] = $options;
147 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'', $morecss, ($disabledajaxcombo ? 0 : 1));
167 public function multi_select_journal($selectedIds = array(), $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'', $usecache =
'', $disabledajaxcombo = 0)
170 global $conf, $langs;
175 if ($usecache && !empty($this->options_cache[$usecache])) {
176 $options = $this->options_cache[$usecache];
177 $selected = $selectedIds;
179 $sql =
"SELECT rowid, code, label, nature, entity, active";
180 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
181 $sql .=
" WHERE active = 1";
182 $sql .=
" AND entity = ".$conf->entity;
183 if ($nature && is_numeric($nature)) {
184 $sql .=
" AND nature = ".((int) $nature);
186 $sql .=
" ORDER BY code";
188 dol_syslog(get_class($this).
"::multi_select_journal", LOG_DEBUG);
189 $resql = $this->db->query($sql);
192 $this->error =
"Error ".$this->db->lasterror();
193 dol_syslog(get_class($this).
"::multi_select_journal ".$this->error, LOG_ERR);
198 $langs->load(
'accountancy');
199 while ($obj = $this->db->fetch_object($resql)) {
200 $label = $langs->trans($obj->label);
202 $select_value_in = $obj->rowid;
203 $select_value_out = $obj->rowid;
206 if ($select_in == 1) {
207 $select_value_in = $obj->code;
209 if ($select_out == 1) {
210 $select_value_out = $obj->code;
213 if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
215 $selected[] = $select_value_out;
217 $options[$select_value_out] = $label;
219 $this->db->free($resql);
222 $this->options_cache[$usecache] = $options;
226 $out .=
Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0,
'',
'code_journal',
'', ($disabledajaxcombo ? 0 : 1));
244 public function select_accounting_category($selected = 0, $htmlname =
'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
247 global $langs, $mysoc;
249 if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) {
250 dol_print_error(
null,
'Call to select_accounting_account with mysoc country not yet defined');
256 if (!empty($mysoc->country_id)) {
257 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
258 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c";
259 $sql .=
" WHERE c.active = 1";
260 $sql .=
" AND c.category_type = 0";
261 if (empty($allcountries)) {
262 $sql .=
" AND c.fk_country = ".((int) $mysoc->country_id);
264 $sql .=
" ORDER BY c.label ASC";
266 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
267 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c, ".$this->db->prefix().
"c_country as co";
268 $sql .=
" WHERE c.active = 1";
269 $sql .=
" AND c.category_type = 0";
270 $sql .=
" AND c.fk_country = co.rowid";
271 if (empty($allcountries)) {
272 $sql .=
" AND co.code = '".$this->db->escape($mysoc->country_code).
"'";
274 $sql .=
" ORDER BY c.label ASC";
277 $this->nbaccounts_category = 0;
279 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
280 $resql = $this->db->query($sql);
282 $num = $this->db->num_rows($resql);
284 $this->nbaccounts_category = $num;
286 $out .=
'<select class="flat minwidth200" id="'.$htmlname.
'" name="'.$htmlname.
'">';
290 $out .=
'<option value="0"> </option>';
293 $obj = $this->db->fetch_object($resql);
295 $titletoshowhtml = ($maxlen ?
dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ?
' <span class="opacitymedium">('.$obj->range_account.
')</span>' :
'');
296 $titletoshow = ($maxlen ?
dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ?
' ('.$obj->range_account.
')' :
'');
298 $out .=
'<option value="'.$obj->rowid.
'"';
299 if ($obj->rowid == $selected) {
303 $out .=
' data-html="'.dolPrintHTMLForAttribute($titletoshowhtml).
'"';
314 $out .=
'<span class="opacitymedium">'.$langs->trans(
"ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->transnoentitiesnoconv(
"Accounting"), $langs->transnoentitiesnoconv(
"Setup"), $langs->transnoentitiesnoconv(
"AccountingCategories")).
'</span>';
336 $sql =
"SELECT DISTINCT import_key FROM ".$this->db->prefix().
"accounting_bookkeeping";
337 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
338 $sql .=
' ORDER BY import_key DESC';
340 dol_syslog(get_class($this).
"::select_bookkeeping_importkey", LOG_DEBUG);
341 $resql = $this->db->query($sql);
344 $this->error =
"Error ".$this->db->lasterror();
345 dol_syslog(get_class($this).
"::select_bookkeeping_importkey ".$this->error, LOG_ERR);
349 while ($obj = $this->db->fetch_object($resql)) {
350 $options[$obj->import_key] = $obj->import_key;
371 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $active =
'1')
374 global $conf, $langs;
376 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
383 if ($showempty == 2) {
384 $options[
'0'] =
'--- '.$langs->trans(
"None").
' ---';
387 if ($usecache && !empty($this->options_cache[$usecache])) {
388 $options += $this->options_cache[$usecache];
389 $selected = $selectid;
391 $trunclength =
getDolGlobalInt(
'ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
393 $sql =
"SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
394 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as aa";
395 $sql .=
" INNER JOIN ".$this->db->prefix().
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
396 $sql .=
" AND asy.rowid = ".((int)
getDolGlobalInt(
'CHARTOFACCOUNTS'));
397 if ($active ===
'1') {
398 $sql .=
" AND aa.active = 1";
399 } elseif ($active ===
'0') {
400 $sql .=
" AND aa.active = 0";
402 $sql .=
" AND aa.entity=".((int) $conf->entity);
403 $sql .=
" ORDER BY aa.account_number";
405 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
406 $resql = $this->db->query($sql);
409 $this->error =
"Error ".$this->db->lasterror();
410 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
414 $num_rows = $this->db->num_rows($resql);
417 $langs->load(
"errors");
418 $showempty = $langs->trans(
"ErrorYouMustFirstSetupYourChartOfAccount");
420 $selected = $selectid;
421 while ($obj = $this->db->fetch_object($resql)) {
422 if (empty($obj->labelshort)) {
423 $labeltoshow = $obj->label;
425 $labeltoshow = $obj->labelshort;
429 $label =
dol_trunc($label, $trunclength);
431 $select_value_in = $obj->rowid;
432 $select_value_out = $obj->rowid;
435 if ($select_in == 1) {
436 $select_value_in = $obj->account_number;
438 if ($select_out == 1) {
439 $select_value_out = $obj->account_number;
443 if ($selectid !=
'' && $selectid == $select_value_in) {
445 $selected = $select_value_out;
448 $options[$select_value_out] = $label;
452 $this->db->free($resql);
455 $this->options_cache[$usecache] = $options;
456 unset($this->options_cache[$usecache][
'0']);
461 $out .=
Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
463 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
480 public function select_auxaccount($selectid, $htmlname =
'account_num_aux', $showempty = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $labelhtmlname =
'')
485 $aux_account = array();
487 if ($usecache && !empty($this->options_cache[$usecache])) {
488 $aux_account += $this->options_cache[$usecache];
490 dol_syslog(get_class($this).
"::select_auxaccount", LOG_DEBUG);
493 $sql =
"SELECT code_compta as code_compta_client, code_compta_fournisseur, nom as name";
494 $sql .=
" FROM ".$this->db->prefix().
"societe";
495 $sql .=
" WHERE entity IN (".getEntity(
'societe').
")";
496 $sql .=
" AND (client IN (1,3) OR fournisseur = 1)";
498 $resql = $this->db->query($sql);
500 while ($obj = $this->db->fetch_object($resql)) {
501 if (!empty($obj->code_compta_client)) {
502 $aux_account[$obj->code_compta_client] = $obj->code_compta_client.
' <span class="opacitymedium">('.$obj->name.
')</span>';
504 if (!empty($obj->code_compta_fournisseur)) {
505 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.
' <span class="opacitymedium">('.$obj->name.
')</span>';
509 $this->error =
"Error ".$this->db->lasterror();
510 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
516 $this->db->free($resql);
519 $sql =
"SELECT DISTINCT accountancy_code, lastname, firstname ";
520 $sql .=
" FROM ".$this->db->prefix().
"user";
521 $sql .=
" WHERE entity IN (".getEntity(
'user').
")";
522 $sql .=
" ORDER BY accountancy_code";
524 $resql = $this->db->query($sql);
526 while ($obj = $this->db->fetch_object($resql)) {
527 if (!empty($obj->accountancy_code)) {
528 $aux_account[$obj->accountancy_code] = $obj->accountancy_code.
' <span class="opacitymedium">('.
dolGetFirstLastname($obj->firstname, $obj->lastname).
')</span>';
532 $this->error =
"Error ".$this->db->lasterror();
533 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
536 $this->db->free($resql);
539 $this->options_cache[$usecache] = $aux_account;
545 $out .=
Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
547 if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
548 $out .=
'<script nonce="'.getNonce().
'">
549 jQuery(document).ready(() => {
550 $("#'.$htmlname.
'").on("select2:select", function(e) {
551 var regExp = /\(([^)]+)\)/;
552 const match = regExp.exec(e.params.data.text);
553 $(\'input[name="'.
dol_escape_js($labelhtmlname).
'"]\').val(match[1]);
578 $out_array = array();
580 $sql =
"SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
581 $sql .=
" FROM ".$this->db->prefix().
"accounting_bookkeeping";
582 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
583 $sql .=
" ORDER BY date_format(doc_date, '%Y')";
585 $resql = $this->db->query($sql);
588 $this->error =
"Error ".$this->db->lasterror();
592 while ($obj = $this->db->fetch_object($resql)) {
593 $out_array[$obj->dtyear] = $obj->dtyear;
595 $this->db->free($resql);
597 if ($output_format ==
'html') {
598 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0,
'placeholder="aa"');
616 public function formAccountingAccount($page, $selected =
'', $htmlname =
'none', $option = 0, $useempty = 1, $filter =
'', $nooutput = 0)
621 if ($htmlname !=
"none") {
622 $out .=
'<form method="post" action="' . $page .
'">';
623 $out .=
'<input type="hidden" name="action" value="set'.$htmlname.
'">';
624 $out .=
'<input type="hidden" name="token" value="' .
newToken() .
'">';
626 $out .= $this->
select_account($selected, $htmlname, $useempty, array(), 1, 1,
'minwidth100 maxwidth300 maxwidthonsmartphone',
'accounts', $filter);
628 $out .= $this->
select_auxaccount($selected, $htmlname, $useempty,
'minwidth100 maxwidth300 maxwidthonsmartphone',
'subaccounts');
630 $out .=
'<input type="submit" class="button smallpaddingimp valignmiddle" name="modify" value="' . $langs->trans(
"Modify") .
'">';
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.
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.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_print_error($db=null, $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 '…' 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...