28require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
39 private $options_cache = array();
58 public $nbaccounts_category;
86 public function select_journal($selectid, $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'maxwidth300 maxwidthonsmartphone', $usecache =
'', $disabledajaxcombo = 0)
94 if ($usecache && !empty($this->options_cache[$usecache])) {
95 $options = $this->options_cache[$usecache];
96 $selected = $selectid;
98 $sql =
"SELECT rowid, code, label, nature, entity, active";
99 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
100 $sql .=
" WHERE active = 1";
101 $sql .=
" AND entity = ".((int) $conf->entity);
102 if ($nature && is_numeric($nature)) {
103 $sql .=
" AND nature = ".((int) $nature);
105 $sql .=
" ORDER BY code";
107 dol_syslog(get_class($this).
"::select_journal", LOG_DEBUG);
108 $resql = $this->db->query($sql);
111 $this->error =
"Error ".$this->db->lasterror();
112 dol_syslog(get_class($this).
"::select_journal ".$this->error, LOG_ERR);
117 $langs->load(
'accountancy');
118 while ($obj = $this->db->fetch_object($resql)) {
119 $label = $obj->code.
' - '.$langs->trans($obj->label);
121 $select_value_in = $obj->rowid;
122 $select_value_out = $obj->rowid;
125 if ($select_in == 1) {
126 $select_value_in = $obj->code;
128 if ($select_out == 1) {
129 $select_value_out = $obj->code;
132 if ($selectid !=
'' && $selectid == $select_value_in) {
134 $selected = $select_value_out;
137 $options[$select_value_out] = $label;
139 $this->db->free($resql);
142 $this->options_cache[$usecache] = $options;
146 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'', $morecss, ($disabledajaxcombo ? 0 : 1));
166 public function multi_select_journal($selectedIds = array(), $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'', $usecache =
'', $disabledajaxcombo = 0)
169 global $conf, $langs;
174 if ($usecache && !empty($this->options_cache[$usecache])) {
175 $options = $this->options_cache[$usecache];
176 $selected = $selectedIds;
178 $sql =
"SELECT rowid, code, label, nature, entity, active";
179 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
180 $sql .=
" WHERE active = 1";
181 $sql .=
" AND entity = ".$conf->entity;
182 if ($nature && is_numeric($nature)) {
183 $sql .=
" AND nature = ".((int) $nature);
185 $sql .=
" ORDER BY code";
187 dol_syslog(get_class($this).
"::multi_select_journal", LOG_DEBUG);
188 $resql = $this->db->query($sql);
191 $this->error =
"Error ".$this->db->lasterror();
192 dol_syslog(get_class($this).
"::multi_select_journal ".$this->error, LOG_ERR);
197 $langs->load(
'accountancy');
198 while ($obj = $this->db->fetch_object($resql)) {
199 $label = $langs->trans($obj->label);
201 $select_value_in = $obj->rowid;
202 $select_value_out = $obj->rowid;
205 if ($select_in == 1) {
206 $select_value_in = $obj->code;
208 if ($select_out == 1) {
209 $select_value_out = $obj->code;
212 if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
214 $selected[] = $select_value_out;
216 $options[$select_value_out] = $label;
218 $this->db->free($resql);
221 $this->options_cache[$usecache] = $options;
225 $out .=
Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0,
'code_journal',
'', ($disabledajaxcombo ? 0 : 1));
243 public function select_accounting_category($selected = 0, $htmlname =
'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
246 global $langs, $mysoc;
248 if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) {
249 dol_print_error(
null,
'Call to select_accounting_account with mysoc country not yet defined');
255 if (!empty($mysoc->country_id)) {
256 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
257 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c";
258 $sql .=
" WHERE c.active = 1";
259 $sql .=
" AND c.category_type = 0";
260 if (empty($allcountries)) {
261 $sql .=
" AND c.fk_country = ".((int) $mysoc->country_id);
263 $sql .=
" ORDER BY c.label ASC";
265 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
266 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c, ".$this->db->prefix().
"c_country as co";
267 $sql .=
" WHERE c.active = 1";
268 $sql .=
" AND c.category_type = 0";
269 $sql .=
" AND c.fk_country = co.rowid";
270 if (empty($allcountries)) {
271 $sql .=
" AND co.code = '".$this->db->escape($mysoc->country_code).
"'";
273 $sql .=
" ORDER BY c.label ASC";
276 $this->nbaccounts_category = 0;
278 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
279 $resql = $this->db->query($sql);
281 $num = $this->db->num_rows($resql);
283 $this->nbaccounts_category = $num;
285 $out .=
'<select class="flat minwidth200" id="'.$htmlname.
'" name="'.$htmlname.
'">';
289 $out .=
'<option value="0"> </option>';
292 $obj = $this->db->fetch_object($resql);
294 $titletoshowhtml = ($maxlen ?
dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ?
' <span class="opacitymedium">('.$obj->range_account.
')</span>' :
'');
295 $titletoshow = ($maxlen ?
dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ?
' ('.$obj->range_account.
')' :
'');
297 $out .=
'<option value="'.$obj->rowid.
'"';
298 if ($obj->rowid == $selected) {
302 $out .=
' data-html="'.dolPrintHTMLForAttribute($titletoshowhtml).
'"';
313 $out .=
'<span class="opacitymedium">'.$langs->trans(
"ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->transnoentitiesnoconv(
"Accounting"), $langs->transnoentitiesnoconv(
"Setup"), $langs->transnoentitiesnoconv(
"AccountingCategories")).
'</span>';
335 $sql =
"SELECT DISTINCT import_key FROM ".$this->db->prefix().
"accounting_bookkeeping";
336 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
337 $sql .=
' ORDER BY import_key DESC';
339 dol_syslog(get_class($this).
"::select_bookkeeping_importkey", LOG_DEBUG);
340 $resql = $this->db->query($sql);
343 $this->error =
"Error ".$this->db->lasterror();
344 dol_syslog(get_class($this).
"::select_bookkeeping_importkey ".$this->error, LOG_ERR);
348 while ($obj = $this->db->fetch_object($resql)) {
349 $options[$obj->import_key] = $obj->import_key;
370 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $active =
'1')
373 global $conf, $langs;
375 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
381 if ($showempty == 2) {
382 $options[
'0'] =
'--- '.$langs->trans(
"None").
' ---';
385 if ($usecache && !empty($this->options_cache[$usecache])) {
386 $options = $options + $this->options_cache[$usecache];
387 $selected = $selectid;
389 $trunclength =
getDolGlobalInt(
'ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
391 $sql =
"SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
392 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as aa";
393 $sql .=
" INNER JOIN ".$this->db->prefix().
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
394 $sql .=
" AND asy.rowid = ".((int)
getDolGlobalInt(
'CHARTOFACCOUNTS'));
395 if ($active ===
'1') {
396 $sql .=
" AND aa.active = 1";
397 } elseif ($active ===
'0') {
398 $sql .=
" AND aa.active = 0";
400 $sql .=
" AND aa.entity=".((int) $conf->entity);
401 $sql .=
" ORDER BY aa.account_number";
403 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
404 $resql = $this->db->query($sql);
407 $this->error =
"Error ".$this->db->lasterror();
408 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
412 $num_rows = $this->db->num_rows($resql);
415 $langs->load(
"errors");
416 $showempty = $langs->trans(
"ErrorYouMustFirstSetupYourChartOfAccount");
418 $selected = $selectid;
419 while ($obj = $this->db->fetch_object($resql)) {
420 if (empty($obj->labelshort)) {
421 $labeltoshow = $obj->label;
423 $labeltoshow = $obj->labelshort;
427 $label =
dol_trunc($label, $trunclength);
429 $select_value_in = $obj->rowid;
430 $select_value_out = $obj->rowid;
433 if ($select_in == 1) {
434 $select_value_in = $obj->account_number;
436 if ($select_out == 1) {
437 $select_value_out = $obj->account_number;
441 if ($selectid !=
'' && $selectid == $select_value_in) {
443 $selected = $select_value_out;
446 $options[$select_value_out] = $label;
450 $this->db->free($resql);
453 $this->options_cache[$usecache] = $options;
454 unset($this->options_cache[$usecache][
'0']);
459 $out .=
Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
461 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
478 public function select_auxaccount($selectid, $htmlname =
'account_num_aux', $showempty = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $labelhtmlname =
'')
483 $aux_account = array();
485 if ($usecache && !empty($this->options_cache[$usecache])) {
486 $aux_account = $aux_account + $this->options_cache[$usecache];
488 dol_syslog(get_class($this).
"::select_auxaccount", LOG_DEBUG);
491 $sql =
"SELECT code_compta, code_compta_fournisseur, nom as name";
492 $sql .=
" FROM ".$this->db->prefix().
"societe";
493 $sql .=
" WHERE entity IN (".getEntity(
'societe').
")";
494 $sql .=
" AND (client IN (1,3) OR fournisseur = 1)";
496 $resql = $this->db->query($sql);
498 while ($obj = $this->db->fetch_object($resql)) {
499 if (!empty($obj->code_compta)) {
500 $aux_account[$obj->code_compta] = $obj->code_compta.
' <span class="opacitymedium">('.$obj->name.
')</span>';
502 if (!empty($obj->code_compta_fournisseur)) {
503 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.
' <span class="opacitymedium">('.$obj->name.
')</span>';
507 $this->error =
"Error ".$this->db->lasterror();
508 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
514 $this->db->free($resql);
517 $sql =
"SELECT DISTINCT accountancy_code, lastname, firstname ";
518 $sql .=
" FROM ".$this->db->prefix().
"user";
519 $sql .=
" WHERE entity IN (".getEntity(
'user').
")";
520 $sql .=
" ORDER BY accountancy_code";
522 $resql = $this->db->query($sql);
524 while ($obj = $this->db->fetch_object($resql)) {
525 if (!empty($obj->accountancy_code)) {
526 $aux_account[$obj->accountancy_code] = $obj->accountancy_code.
' <span class="opacitymedium">('.
dolGetFirstLastname($obj->firstname, $obj->lastname).
')</span>';
530 $this->error =
"Error ".$this->db->lasterror();
531 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
534 $this->db->free($resql);
537 $this->options_cache[$usecache] = $aux_account;
543 $out .=
Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
545 if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
546 $out .=
'<script nonce="'.getNonce().
'">
547 jQuery(document).ready(() => {
548 $("#'.$htmlname.
'").on("select2:select", function(e) {
549 var regExp = /\(([^)]+)\)/;
550 const match = regExp.exec(e.params.data.text);
551 $(\'input[name="'.
dol_escape_js($labelhtmlname).
'"]\').val(match[1]);
576 $out_array = array();
578 $sql =
"SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
579 $sql .=
" FROM ".$this->db->prefix().
"accounting_bookkeeping";
580 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
581 $sql .=
" ORDER BY date_format(doc_date, '%Y')";
583 $resql = $this->db->query($sql);
586 $this->error =
"Error ".$this->db->lasterror();
590 while ($obj = $this->db->fetch_object($resql)) {
591 $out_array[$obj->dtyear] = $obj->dtyear;
593 $this->db->free($resql);
595 if ($output_format ==
'html') {
596 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0,
'placeholder="aa"');
614 public function formAccountingAccount($page, $selected =
'', $htmlname =
'none', $option = 0, $useempty = 1, $filter =
'', $nooutput = 0)
619 if ($htmlname !=
"none") {
620 $out .=
'<form method="post" action="' . $page .
'">';
621 $out .=
'<input type="hidden" name="action" value="set'.$htmlname.
'">';
622 $out .=
'<input type="hidden" name="token" value="' .
newToken() .
'">';
624 $out .= $this->
select_account($selected, $htmlname, $useempty, array(), 1, 1,
'minwidth100 maxwidth300 maxwidthonsmartphone',
'accounts', $filter);
626 $out .= $this->
select_auxaccount($selected, $htmlname, $useempty,
'minwidth100 maxwidth300 maxwidthonsmartphone',
'subaccounts');
628 $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...