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) {
304 $out = $langs->trans(
"ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->trans(
"Accounting"), $langs->trans(
"Setup"), $langs->trans(
"AccountingCategories"));
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';
332 dol_syslog(get_class($this).
"::select_bookkeeping_importkey", LOG_DEBUG);
333 $resql = $this->db->query($sql);
336 $this->error =
"Error ".$this->db->lasterror();
337 dol_syslog(get_class($this).
"::select_bookkeeping_importkey ".$this->error, LOG_ERR);
341 while ($obj = $this->db->fetch_object($resql)) {
342 $options[$obj->import_key] = $obj->import_key;
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')
366 global $conf, $langs;
368 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
374 if ($showempty == 2) {
375 $options[
'0'] =
'--- '.$langs->trans(
"None").
' ---';
378 if ($usecache && !empty($this->options_cache[$usecache])) {
379 $options = $options + $this->options_cache[$usecache];
380 $selected = $selectid;
382 $trunclength =
getDolGlobalInt(
'ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
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";
393 $sql .=
" AND aa.entity=".((int) $conf->entity);
394 $sql .=
" ORDER BY aa.account_number";
396 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
397 $resql = $this->db->query($sql);
400 $this->error =
"Error ".$this->db->lasterror();
401 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
405 $num_rows = $this->db->num_rows($resql);
408 $langs->load(
"errors");
409 $showempty = $langs->trans(
"ErrorYouMustFirstSetupYourChartOfAccount");
411 $selected = $selectid;
412 while ($obj = $this->db->fetch_object($resql)) {
413 if (empty($obj->labelshort)) {
414 $labeltoshow = $obj->label;
416 $labeltoshow = $obj->labelshort;
420 $label =
dol_trunc($label, $trunclength);
422 $select_value_in = $obj->rowid;
423 $select_value_out = $obj->rowid;
426 if ($select_in == 1) {
427 $select_value_in = $obj->account_number;
429 if ($select_out == 1) {
430 $select_value_out = $obj->account_number;
434 if ($selectid !=
'' && $selectid == $select_value_in) {
436 $selected = $select_value_out;
439 $options[$select_value_out] = $label;
443 $this->db->free($resql);
446 $this->options_cache[$usecache] = $options;
447 unset($this->options_cache[$usecache][
'0']);
452 $out .=
Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
454 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
471 public function select_auxaccount($selectid, $htmlname =
'account_num_aux', $showempty = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $labelhtmlname =
'')
476 $aux_account = array();
478 if ($usecache && !empty($this->options_cache[$usecache])) {
479 $aux_account = $aux_account + $this->options_cache[$usecache];
481 dol_syslog(get_class($this).
"::select_auxaccount", LOG_DEBUG);
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)";
489 $resql = $this->db->query($sql);
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>';
495 if (!empty($obj->code_compta_fournisseur)) {
496 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.
' <span class="opacitymedium">('.$obj->name.
')</span>';
500 $this->error =
"Error ".$this->db->lasterror();
501 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
507 $this->db->free($resql);
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";
515 $resql = $this->db->query($sql);
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>';
523 $this->error =
"Error ".$this->db->lasterror();
524 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
527 $this->db->free($resql);
530 $this->options_cache[$usecache] = $aux_account;
536 $out .=
Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
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]);
569 $out_array = array();
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')";
576 $resql = $this->db->query($sql);
579 $this->error =
"Error ".$this->db->lasterror();
583 while ($obj = $this->db->fetch_object($resql)) {
584 $out_array[$obj->dtyear] = $obj->dtyear;
586 $this->db->free($resql);
588 if ($output_format ==
'html') {
589 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 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...