27require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
35 private $options_cache = array();
54 public $nbaccounts_category;
82 public function select_journal($selectid, $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'maxwidth300 maxwidthonsmartphone', $usecache =
'', $disabledajaxcombo = 0)
90 if ($usecache && !empty($this->options_cache[$usecache])) {
91 $options = $this->options_cache[$usecache];
92 $selected = $selectid;
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);
101 $sql .=
" ORDER BY code";
103 dol_syslog(get_class($this).
"::select_journal", LOG_DEBUG);
104 $resql = $this->db->query($sql);
107 $this->error =
"Error ".$this->db->lasterror();
108 dol_syslog(get_class($this).
"::select_journal ".$this->error, LOG_ERR);
113 $langs->load(
'accountancy');
114 while ($obj = $this->db->fetch_object($resql)) {
115 $label = $obj->code.
' - '.$langs->trans($obj->label);
117 $select_value_in = $obj->rowid;
118 $select_value_out = $obj->rowid;
121 if ($select_in == 1) {
122 $select_value_in = $obj->code;
124 if ($select_out == 1) {
125 $select_value_out = $obj->code;
128 if ($selectid !=
'' && $selectid == $select_value_in) {
130 $selected = $select_value_out;
133 $options[$select_value_out] = $label;
135 $this->db->free($resql);
138 $this->options_cache[$usecache] = $options;
142 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'', $morecss, ($disabledajaxcombo ? 0 : 1));
162 public function multi_select_journal($selectedIds = array(), $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'', $usecache =
'', $disabledajaxcombo = 0)
165 global $conf, $langs;
170 if ($usecache && !empty($this->options_cache[$usecache])) {
171 $options = $this->options_cache[$usecache];
172 $selected = $selectedIds;
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);
181 $sql .=
" ORDER BY code";
183 dol_syslog(get_class($this).
"::multi_select_journal", LOG_DEBUG);
184 $resql = $this->db->query($sql);
187 $this->error =
"Error ".$this->db->lasterror();
188 dol_syslog(get_class($this).
"::multi_select_journal ".$this->error, LOG_ERR);
193 $langs->load(
'accountancy');
194 while ($obj = $this->db->fetch_object($resql)) {
195 $label = $langs->trans($obj->label);
197 $select_value_in = $obj->rowid;
198 $select_value_out = $obj->rowid;
201 if ($select_in == 1) {
202 $select_value_in = $obj->code;
204 if ($select_out == 1) {
205 $select_value_out = $obj->code;
208 if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
210 $selected[] = $select_value_out;
212 $options[$select_value_out] = $label;
214 $this->db->free($resql);
217 $this->options_cache[$usecache] = $options;
221 $out .=
Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0,
'code_journal',
'', ($disabledajaxcombo ? 0 : 1));
239 public function select_accounting_category($selected =
'', $htmlname =
'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
242 global $langs, $mysoc;
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');
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);
257 $sql .=
" ORDER BY c.label ASC";
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).
"'";
267 $sql .=
" ORDER BY c.label ASC";
270 $this->nbaccounts_category = 0;
272 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
273 $resql = $this->db->query($sql);
275 $num = $this->db->num_rows($resql);
277 $this->nbaccounts_category = $num;
279 $out =
'<select class="flat minwidth200" id="'.$htmlname.
'" name="'.$htmlname.
'">';
283 $out .=
'<option value="0"> </option>';
286 $obj = $this->db->fetch_object($resql);
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.
')' :
'');
291 $out .=
'<option value="'.$obj->rowid.
'"';
292 if ($obj->rowid == $selected) {
296 $out .=
' data-html="'.dolPrintHTMLForAttribute($titletoshowhtml).
'"';
305 $out = $langs->trans(
"ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->transnoentitiesnoconv(
"Accounting"), $langs->transnoentitiesnoconv(
"Setup"), $langs->transnoentitiesnoconv(
"AccountingCategories"));
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';
333 dol_syslog(get_class($this).
"::select_bookkeeping_importkey", LOG_DEBUG);
334 $resql = $this->db->query($sql);
337 $this->error =
"Error ".$this->db->lasterror();
338 dol_syslog(get_class($this).
"::select_bookkeeping_importkey ".$this->error, LOG_ERR);
342 while ($obj = $this->db->fetch_object($resql)) {
343 $options[$obj->import_key] = $obj->import_key;
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')
367 global $conf, $langs;
369 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
375 if ($showempty == 2) {
376 $options[
'0'] =
'--- '.$langs->trans(
"None").
' ---';
379 if ($usecache && !empty($this->options_cache[$usecache])) {
380 $options = $options + $this->options_cache[$usecache];
381 $selected = $selectid;
383 $trunclength =
getDolGlobalInt(
'ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
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";
394 $sql .=
" AND aa.entity=".((int) $conf->entity);
395 $sql .=
" ORDER BY aa.account_number";
397 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
398 $resql = $this->db->query($sql);
401 $this->error =
"Error ".$this->db->lasterror();
402 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
406 $num_rows = $this->db->num_rows($resql);
409 $langs->load(
"errors");
410 $showempty = $langs->trans(
"ErrorYouMustFirstSetupYourChartOfAccount");
412 $selected = $selectid;
413 while ($obj = $this->db->fetch_object($resql)) {
414 if (empty($obj->labelshort)) {
415 $labeltoshow = $obj->label;
417 $labeltoshow = $obj->labelshort;
421 $label =
dol_trunc($label, $trunclength);
423 $select_value_in = $obj->rowid;
424 $select_value_out = $obj->rowid;
427 if ($select_in == 1) {
428 $select_value_in = $obj->account_number;
430 if ($select_out == 1) {
431 $select_value_out = $obj->account_number;
435 if ($selectid !=
'' && $selectid == $select_value_in) {
437 $selected = $select_value_out;
440 $options[$select_value_out] = $label;
444 $this->db->free($resql);
447 $this->options_cache[$usecache] = $options;
448 unset($this->options_cache[$usecache][
'0']);
453 $out .=
Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
455 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
472 public function select_auxaccount($selectid, $htmlname =
'account_num_aux', $showempty = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $labelhtmlname =
'')
477 $aux_account = array();
479 if ($usecache && !empty($this->options_cache[$usecache])) {
480 $aux_account = $aux_account + $this->options_cache[$usecache];
482 dol_syslog(get_class($this).
"::select_auxaccount", LOG_DEBUG);
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)";
490 $resql = $this->db->query($sql);
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>';
496 if (!empty($obj->code_compta_fournisseur)) {
497 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.
' <span class="opacitymedium">('.$obj->name.
')</span>';
501 $this->error =
"Error ".$this->db->lasterror();
502 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
508 $this->db->free($resql);
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";
516 $resql = $this->db->query($sql);
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>';
524 $this->error =
"Error ".$this->db->lasterror();
525 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
528 $this->db->free($resql);
531 $this->options_cache[$usecache] = $aux_account;
537 $out .=
Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty) : 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
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]);
570 $out_array = array();
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')";
577 $resql = $this->db->query($sql);
580 $this->error =
"Error ".$this->db->lasterror();
584 while ($obj = $this->db->fetch_object($resql)) {
585 $out_array[$obj->dtyear] = $obj->dtyear;
587 $this->db->free($resql);
589 if ($output_format ==
'html') {
590 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0,
'placeholder="aa"');
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.
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...