27 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
35 private $options_cache = array();
72 public function select_journal($selectid, $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'maxwidth300 maxwidthonsmartphone', $usecache =
'', $disabledajaxcombo = 0)
80 if ($usecache && !empty($this->options_cache[$usecache])) {
81 $options = $this->options_cache[$usecache];
82 $selected = $selectid;
84 $sql =
"SELECT rowid, code, label, nature, entity, active";
85 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
86 $sql .=
" WHERE active = 1";
87 $sql .=
" AND entity = ".$conf->entity;
88 if ($nature && is_numeric($nature)) {
89 $sql .=
" AND nature = ".((int) $nature);
91 $sql .=
" ORDER BY code";
93 dol_syslog(get_class($this).
"::select_journal", LOG_DEBUG);
97 $this->error =
"Error ".$this->db->lasterror();
98 dol_syslog(get_class($this).
"::select_journal ".$this->error, LOG_ERR);
103 $langs->load(
'accountancy');
104 while ($obj = $this->
db->fetch_object(
$resql)) {
105 $label = $obj->code.
' - '.$langs->trans($obj->label);
107 $select_value_in = $obj->rowid;
108 $select_value_out = $obj->rowid;
111 if ($select_in == 1) {
112 $select_value_in = $obj->code;
114 if ($select_out == 1) {
115 $select_value_out = $obj->code;
118 if ($selectid !=
'' && $selectid == $select_value_in) {
120 $selected = $select_value_out;
123 $options[$select_value_out] = $label;
128 $this->options_cache[$usecache] = $options;
132 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'', $morecss, ($disabledajaxcombo ? 0 : 1));
152 public function multi_select_journal($selectedIds = array(), $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'', $usecache =
'', $disabledajaxcombo = 0)
155 global $conf, $langs;
160 if ($usecache && !empty($this->options_cache[$usecache])) {
161 $options = $this->options_cache[$usecache];
162 $selected = $selectedIds;
164 $sql =
"SELECT rowid, code, label, nature, entity, active";
165 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
166 $sql .=
" WHERE active = 1";
167 $sql .=
" AND entity = ".$conf->entity;
168 if ($nature && is_numeric($nature)) {
169 $sql .=
" AND nature = ".((int) $nature);
171 $sql .=
" ORDER BY code";
173 dol_syslog(get_class($this).
"::multi_select_journal", LOG_DEBUG);
177 $this->error =
"Error ".$this->db->lasterror();
178 dol_syslog(get_class($this).
"::multi_select_journal ".$this->error, LOG_ERR);
183 $langs->load(
'accountancy');
184 while ($obj = $this->
db->fetch_object(
$resql)) {
185 $label = $langs->trans($obj->label);
187 $select_value_in = $obj->rowid;
188 $select_value_out = $obj->rowid;
191 if ($select_in == 1) {
192 $select_value_in = $obj->code;
194 if ($select_out == 1) {
195 $select_value_out = $obj->code;
198 if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
200 $selected[] = $select_value_out;
202 $options[$select_value_out] = $label;
207 $this->options_cache[$usecache] = $options;
211 $out .=
Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0,
'code_journal',
'', ($disabledajaxcombo ? 0 : 1));
229 public function select_accounting_category($selected =
'', $htmlname =
'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
232 global $db, $langs, $user, $mysoc;
234 if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) {
235 dol_print_error(
'',
'Call to select_accounting_account with mysoc country not yet defined');
239 if (!empty($mysoc->country_id)) {
240 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
241 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c";
242 $sql .=
" WHERE c.active = 1";
243 $sql .=
" AND c.category_type = 0";
244 if (empty($allcountries)) {
245 $sql .=
" AND c.fk_country = ".((int) $mysoc->country_id);
247 $sql .=
" ORDER BY c.label ASC";
249 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
250 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c, ".$this->
db->prefix().
"c_country as co";
251 $sql .=
" WHERE c.active = 1";
252 $sql .=
" AND c.category_type = 0";
253 $sql .=
" AND c.fk_country = co.rowid";
254 if (empty($allcountries)) {
255 $sql .=
" AND co.code = '".$this->db->escape($mysoc->country_code).
"'";
257 $sql .=
" ORDER BY c.label ASC";
260 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
265 $out =
'<select class="flat minwidth200" id="'.$htmlname.
'" name="'.$htmlname.
'">';
269 $out .=
'<option value="0"> </option>';
272 $obj = $this->
db->fetch_object(
$resql);
273 $out .=
'<option value="'.$obj->rowid.
'"';
274 if ($obj->rowid == $selected) {
286 $out = $langs->trans(
"ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code);
310 $sql =
"SELECT DISTINCT import_key FROM ".$this->db->prefix().
"accounting_bookkeeping";
311 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
312 $sql .=
' ORDER BY import_key DESC';
314 dol_syslog(get_class($this).
"::select_bookkeeping_importkey", LOG_DEBUG);
318 $this->error =
"Error ".$this->db->lasterror();
319 dol_syslog(get_class($this).
"::select_bookkeeping_importkey ".$this->error, LOG_ERR);
323 while ($obj = $this->
db->fetch_object(
$resql)) {
324 $options[$obj->import_key] = $obj->import_key;
344 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'')
347 global $conf, $langs;
349 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
355 if ($showempty == 2) {
356 $options[
'0'] =
'--- '.$langs->trans(
"None").
' ---';
359 if ($usecache && !empty($this->options_cache[$usecache])) {
360 $options = $options + $this->options_cache[$usecache];
361 $selected = $selectid;
363 $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT) ? 50 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT;
365 $sql =
"SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
366 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as aa";
367 $sql .=
" INNER JOIN ".$this->db->prefix().
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
368 $sql .=
" AND asy.rowid = ".((int) $conf->global->CHARTOFACCOUNTS);
369 $sql .=
" AND aa.active = 1";
370 $sql .=
" AND aa.entity=".$conf->entity;
371 $sql .=
" ORDER BY aa.account_number";
373 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
377 $this->error =
"Error ".$this->db->lasterror();
378 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
382 $num_rows = $this->
db->num_rows(
$resql);
384 if ($num_rows == 0 && (empty($conf->global->CHARTOFACCOUNTS) || $conf->global->CHARTOFACCOUNTS < 0)) {
385 $langs->load(
"errors");
386 $showempty = $langs->trans(
"ErrorYouMustFirstSetupYourChartOfAccount");
388 $selected = $selectid;
389 while ($obj = $this->
db->fetch_object(
$resql)) {
390 if (empty($obj->labelshort)) {
391 $labeltoshow = $obj->label;
393 $labeltoshow = $obj->labelshort;
397 $label =
dol_trunc($label, $trunclength);
399 $select_value_in = $obj->rowid;
400 $select_value_out = $obj->rowid;
403 if ($select_in == 1) {
404 $select_value_in = $obj->account_number;
406 if ($select_out == 1) {
407 $select_value_out = $obj->account_number;
411 if ($selectid !=
'' && $selectid == $select_value_in) {
413 $selected = $select_value_out;
416 $options[$select_value_out] = $label;
423 $this->options_cache[$usecache] = $options;
424 unset($this->options_cache[$usecache][
'0']);
428 $out .=
Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
445 public function select_auxaccount($selectid, $htmlname =
'account_num_aux', $showempty = 0, $morecss =
'maxwidth250', $usecache =
'', $labelhtmlname =
'')
449 $aux_account = array();
451 if ($usecache && !empty($this->options_cache[$usecache])) {
452 $aux_account = $aux_account + $this->options_cache[$usecache];
454 dol_syslog(get_class($this).
"::select_auxaccount", LOG_DEBUG);
457 $sql =
"SELECT code_compta, code_compta_fournisseur, nom as name";
458 $sql .=
" FROM ".$this->db->prefix().
"societe";
459 $sql .=
" WHERE entity IN (".getEntity(
'societe').
")";
460 $sql .=
" AND (client IN (1,3) OR fournisseur = 1)";
464 while ($obj = $this->
db->fetch_object(
$resql)) {
465 if (!empty($obj->code_compta)) {
466 $aux_account[$obj->code_compta] = $obj->code_compta.
' <span class="opacitymedium">('.$obj->name.
')</span>';
468 if (!empty($obj->code_compta_fournisseur)) {
469 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.
' <span class="opacitymedium">('.$obj->name.
')</span>';
473 $this->error =
"Error ".$this->db->lasterror();
474 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
483 $sql =
"SELECT DISTINCT accountancy_code, lastname, firstname ";
484 $sql .=
" FROM ".$this->db->prefix().
"user";
485 $sql .=
" WHERE entity IN (".getEntity(
'user').
")";
486 $sql .=
" ORDER BY accountancy_code";
490 while ($obj = $this->
db->fetch_object(
$resql)) {
491 if (!empty($obj->accountancy_code)) {
492 $aux_account[$obj->accountancy_code] = $obj->accountancy_code.
' <span class="opacitymedium">('.
dolGetFirstLastname($obj->firstname, $obj->lastname).
')</span>';
496 $this->error =
"Error ".$this->db->lasterror();
497 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
503 $this->options_cache[$usecache] = $aux_account;
509 $out .=
Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
511 if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
513 jQuery(document).ready(() => {
514 $("#'.$htmlname.
'").on("select2:select", function(e) {
515 var regExp = /\(([^)]+)\)/;
516 const match = regExp.exec(e.params.data.text);
517 $(\'input[name="'.
dol_escape_js($labelhtmlname).
'"]\').val(match[1]);
542 $out_array = array();
544 $sql =
"SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
545 $sql .=
" FROM ".$this->db->prefix().
"accounting_bookkeeping";
546 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
547 $sql .=
" ORDER BY date_format(doc_date, '%Y')";
552 $this->error =
"Error ".$this->db->lasterror();
556 while ($obj = $this->
db->fetch_object(
$resql)) {
557 $out_array[$obj->dtyear] = $obj->dtyear;
561 if ($output_format ==
'html') {
562 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0,
'placeholder="aa"');