27 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
35 private $options_cache = array();
78 public function select_journal($selectid, $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'maxwidth300 maxwidthonsmartphone', $usecache =
'', $disabledajaxcombo = 0)
86 if ($usecache && !empty($this->options_cache[$usecache])) {
87 $options = $this->options_cache[$usecache];
88 $selected = $selectid;
90 $sql =
"SELECT rowid, code, label, nature, entity, active";
91 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
92 $sql .=
" WHERE active = 1";
93 $sql .=
" AND entity = ".$conf->entity;
94 if ($nature && is_numeric($nature)) {
95 $sql .=
" AND nature = ".((int) $nature);
97 $sql .=
" ORDER BY code";
99 dol_syslog(get_class($this).
"::select_journal", LOG_DEBUG);
100 $resql = $this->db->query(
$sql);
103 $this->error =
"Error ".$this->db->lasterror();
104 dol_syslog(get_class($this).
"::select_journal ".$this->error, LOG_ERR);
109 $langs->load(
'accountancy');
110 while ($obj = $this->db->fetch_object($resql)) {
111 $label = $obj->code.
' - '.$langs->trans($obj->label);
113 $select_value_in = $obj->rowid;
114 $select_value_out = $obj->rowid;
117 if ($select_in == 1) {
118 $select_value_in = $obj->code;
120 if ($select_out == 1) {
121 $select_value_out = $obj->code;
124 if ($selectid !=
'' && $selectid == $select_value_in) {
126 $selected = $select_value_out;
129 $options[$select_value_out] = $label;
131 $this->db->free($resql);
134 $this->options_cache[$usecache] = $options;
138 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'', $morecss, ($disabledajaxcombo ? 0 : 1));
158 public function multi_select_journal($selectedIds = array(), $htmlname =
'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss =
'', $usecache =
'', $disabledajaxcombo = 0)
161 global $conf, $langs;
166 if ($usecache && !empty($this->options_cache[$usecache])) {
167 $options = $this->options_cache[$usecache];
168 $selected = $selectedIds;
170 $sql =
"SELECT rowid, code, label, nature, entity, active";
171 $sql .=
" FROM ".$this->db->prefix().
"accounting_journal";
172 $sql .=
" WHERE active = 1";
173 $sql .=
" AND entity = ".$conf->entity;
174 if ($nature && is_numeric($nature)) {
175 $sql .=
" AND nature = ".((int) $nature);
177 $sql .=
" ORDER BY code";
179 dol_syslog(get_class($this).
"::multi_select_journal", LOG_DEBUG);
180 $resql = $this->db->query(
$sql);
183 $this->error =
"Error ".$this->db->lasterror();
184 dol_syslog(get_class($this).
"::multi_select_journal ".$this->error, LOG_ERR);
189 $langs->load(
'accountancy');
190 while ($obj = $this->db->fetch_object($resql)) {
191 $label = $langs->trans($obj->label);
193 $select_value_in = $obj->rowid;
194 $select_value_out = $obj->rowid;
197 if ($select_in == 1) {
198 $select_value_in = $obj->code;
200 if ($select_out == 1) {
201 $select_value_out = $obj->code;
204 if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) {
206 $selected[] = $select_value_out;
208 $options[$select_value_out] = $label;
210 $this->db->free($resql);
213 $this->options_cache[$usecache] = $options;
217 $out .=
Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0,
'code_journal',
'', ($disabledajaxcombo ? 0 : 1));
235 public function select_accounting_category($selected =
'', $htmlname =
'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
238 global $db, $langs, $user, $mysoc;
240 if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) {
241 dol_print_error(
'',
'Call to select_accounting_account with mysoc country not yet defined');
245 if (!empty($mysoc->country_id)) {
246 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
247 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c";
248 $sql .=
" WHERE c.active = 1";
249 $sql .=
" AND c.category_type = 0";
250 if (empty($allcountries)) {
251 $sql .=
" AND c.fk_country = ".((int) $mysoc->country_id);
253 $sql .=
" ORDER BY c.label ASC";
255 $sql =
"SELECT c.rowid, c.label as type, c.range_account";
256 $sql .=
" FROM ".$this->db->prefix().
"c_accounting_category as c, ".$this->db->prefix().
"c_country as co";
257 $sql .=
" WHERE c.active = 1";
258 $sql .=
" AND c.category_type = 0";
259 $sql .=
" AND c.fk_country = co.rowid";
260 if (empty($allcountries)) {
261 $sql .=
" AND co.code = '".$this->db->escape($mysoc->country_code).
"'";
263 $sql .=
" ORDER BY c.label ASC";
266 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
267 $resql = $this->db->query(
$sql);
269 $num = $this->db->num_rows($resql);
271 $out =
'<select class="flat minwidth200" id="'.$htmlname.
'" name="'.$htmlname.
'">';
275 $out .=
'<option value="0"> </option>';
278 $obj = $this->db->fetch_object($resql);
280 $titletoshowhtml = ($maxlen ?
dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ?
' <span class="opacitymedium">('.$obj->range_account.
')</span>' :
'');
281 $titletoshow = ($maxlen ?
dol_trunc($obj->type, $maxlen) : $obj->type).($obj->range_account ?
' ('.$obj->range_account.
')' :
'');
283 $out .=
'<option value="'.$obj->rowid.
'"';
284 if ($obj->rowid == $selected) {
296 $out = $langs->trans(
"ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code);
320 $sql =
"SELECT DISTINCT import_key FROM ".$this->db->prefix().
"accounting_bookkeeping";
321 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
322 $sql .=
' ORDER BY import_key DESC';
324 dol_syslog(get_class($this).
"::select_bookkeeping_importkey", LOG_DEBUG);
325 $resql = $this->db->query(
$sql);
328 $this->error =
"Error ".$this->db->lasterror();
329 dol_syslog(get_class($this).
"::select_bookkeeping_importkey ".$this->error, LOG_ERR);
333 while ($obj = $this->db->fetch_object($resql)) {
334 $options[$obj->import_key] = $obj->import_key;
355 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $active =
'1')
358 global $conf, $langs;
360 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
366 if ($showempty == 2) {
367 $options[
'0'] =
'--- '.$langs->trans(
"None").
' ---';
370 if ($usecache && !empty($this->options_cache[$usecache])) {
371 $options = $options + $this->options_cache[$usecache];
372 $selected = $selectid;
374 $trunclength =
getDolGlobalInt(
'ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT', 50);
376 $sql =
"SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version";
377 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as aa";
378 $sql .=
" INNER JOIN ".$this->db->prefix().
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
380 if ($active ===
'1') {
381 $sql .=
" AND aa.active = 1";
382 } elseif ($active ===
'0') {
383 $sql .=
" AND aa.active = 0";
385 $sql .=
" AND aa.entity=".((int) $conf->entity);
386 $sql .=
" ORDER BY aa.account_number";
388 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
389 $resql = $this->db->query(
$sql);
392 $this->error =
"Error ".$this->db->lasterror();
393 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
397 $num_rows = $this->db->num_rows($resql);
400 $langs->load(
"errors");
401 $showempty = $langs->trans(
"ErrorYouMustFirstSetupYourChartOfAccount");
403 $selected = $selectid;
404 while ($obj = $this->db->fetch_object($resql)) {
405 if (empty($obj->labelshort)) {
406 $labeltoshow = $obj->label;
408 $labeltoshow = $obj->labelshort;
412 $label =
dol_trunc($label, $trunclength);
414 $select_value_in = $obj->rowid;
415 $select_value_out = $obj->rowid;
418 if ($select_in == 1) {
419 $select_value_in = $obj->account_number;
421 if ($select_out == 1) {
422 $select_value_out = $obj->account_number;
426 if ($selectid !=
'' && $selectid == $select_value_in) {
428 $selected = $select_value_out;
431 $options[$select_value_out] = $label;
435 $this->db->free($resql);
438 $this->options_cache[$usecache] = $options;
439 unset($this->options_cache[$usecache][
'0']);
444 $out .=
Form::selectarray($htmlname, $options, $selected, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
446 $this->nbaccounts = count($options) - ($showempty == 2 ? 1 : 0);
463 public function select_auxaccount($selectid, $htmlname =
'account_num_aux', $showempty = 0, $morecss =
'minwidth100 maxwidth300 maxwidthonsmartphone', $usecache =
'', $labelhtmlname =
'')
468 $aux_account = array();
470 if ($usecache && !empty($this->options_cache[$usecache])) {
471 $aux_account = $aux_account + $this->options_cache[$usecache];
473 dol_syslog(get_class($this).
"::select_auxaccount", LOG_DEBUG);
476 $sql =
"SELECT code_compta, code_compta_fournisseur, nom as name";
477 $sql .=
" FROM ".$this->db->prefix().
"societe";
478 $sql .=
" WHERE entity IN (".getEntity(
'societe').
")";
479 $sql .=
" AND (client IN (1,3) OR fournisseur = 1)";
481 $resql = $this->db->query(
$sql);
483 while ($obj = $this->db->fetch_object($resql)) {
484 if (!empty($obj->code_compta)) {
485 $aux_account[$obj->code_compta] = $obj->code_compta.
' <span class="opacitymedium">('.$obj->name.
')</span>';
487 if (!empty($obj->code_compta_fournisseur)) {
488 $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.
' <span class="opacitymedium">('.$obj->name.
')</span>';
492 $this->error =
"Error ".$this->db->lasterror();
493 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
499 $this->db->free($resql);
502 $sql =
"SELECT DISTINCT accountancy_code, lastname, firstname ";
503 $sql .=
" FROM ".$this->db->prefix().
"user";
504 $sql .=
" WHERE entity IN (".getEntity(
'user').
")";
505 $sql .=
" ORDER BY accountancy_code";
507 $resql = $this->db->query(
$sql);
509 while ($obj = $this->db->fetch_object($resql)) {
510 if (!empty($obj->accountancy_code)) {
511 $aux_account[$obj->accountancy_code] = $obj->accountancy_code.
' <span class="opacitymedium">('.
dolGetFirstLastname($obj->firstname, $obj->lastname).
')</span>';
515 $this->error =
"Error ".$this->db->lasterror();
516 dol_syslog(get_class($this).
"::select_auxaccount ".$this->error, LOG_ERR);
519 $this->db->free($resql);
522 $this->options_cache[$usecache] = $aux_account;
528 $out .=
Form::selectarray($htmlname, $aux_account, $selectid, ($showempty ? (is_numeric($showempty) ? 1 : $showempty): 0), 0, 0,
'', 0, 0, 0,
'', $morecss, 1);
530 if (!empty($conf->use_javascript_ajax) && !empty($labelhtmlname)) {
531 $out .=
'<script nonce="'.getNonce().
'">
532 jQuery(document).ready(() => {
533 $("#'.$htmlname.
'").on("select2:select", function(e) {
534 var regExp = /\(([^)]+)\)/;
535 const match = regExp.exec(e.params.data.text);
536 $(\'input[name="'.
dol_escape_js($labelhtmlname).
'"]\').val(match[1]);
561 $out_array = array();
563 $sql =
"SELECT DISTINCT date_format(doc_date, '%Y') as dtyear";
564 $sql .=
" FROM ".$this->db->prefix().
"accounting_bookkeeping";
565 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
566 $sql .=
" ORDER BY date_format(doc_date, '%Y')";
568 $resql = $this->db->query(
$sql);
571 $this->error =
"Error ".$this->db->lasterror();
575 while ($obj = $this->db->fetch_object($resql)) {
576 $out_array[$obj->dtyear] = $obj->dtyear;
578 $this->db->free($resql);
580 if ($output_format ==
'html') {
581 return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0,
'placeholder="aa"');