dolibarr 24.0.0-beta
auxaccount.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
25// Minimum ajax defines
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', 1); // Disables token renewal
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1');
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', '1');
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', '1');
37}
38if (!defined('NOHEADERNOFOOTER')) {
39 define('NOHEADERNOFOOTER', '1');
40}
41
42require '../../main.inc.php';
43
51// Security — must be logged in
52if (!$user->id) {
54}
55
56// Security
57if (!isModEnabled('accounting') || !$user->hasRight('accounting', 'mouvements', 'lire')) {
59}
60
61$htmlname = GETPOST('htmlname', 'aZ09') ?: 'account_num_aux';
62$searchkey = GETPOST(str_replace('.', '_', $htmlname), 'alpha');
63$outjson = GETPOSTINT('outjson');
64$limit = getDolGlobalInt('ACCOUNTANCY_AUXACCOUNT_SEARCH_LIMIT', 100);
65$minLength = getDolGlobalInt('ACCOUNTANCY_AUXACCOUNT_USE_SEARCH_TO_SELECT', 2) - 1;
66
67$results = array();
72if (strlen($searchkey) >= $minLength) {
73 // Search anywhere by default (use LIKE '%term%')
74 $prefix = getDolGlobalString('ACCOUNTANCY_AUXACCOUNT_DONOTSEARCH_ANYWHERE') ? '' : '%';
75
76 // --- Thirdparties (customers and/or suppliers) ---
77 $sql = "SELECT code_compta AS code_client, code_compta_fournisseur AS code_fourn, nom AS name";
78 $sql .= " FROM ".$db->prefix()."societe";
79 $sql .= " WHERE entity IN (".getEntity('societe').")";
80 $sql .= " AND (client IN (1,3) OR fournisseur = 1)";
81 $sql .= " AND (code_compta LIKE '".$db->escape($prefix.$searchkey)."%'";
82 $sql .= " OR code_compta_fournisseur LIKE '".$db->escape($prefix.$searchkey)."%'";
83 $sql .= " OR nom LIKE '".$db->escape($prefix.$searchkey)."%')";
84 $sql .= $db->order("nom", "ASC");
85 $sql .= $db->plimit($limit, 0);
86
87 $resql = $db->query($sql);
88 if ($resql) {
89 while ($obj = $db->fetch_object($resql)) {
90 if (!empty($obj->code_client)) {
91 $key = $obj->code_client;
92 $results[$key] = array(
93 'key' => $key,
94 'value' => $key.' ('.$obj->name.')', // value shown in input once selected
95 'label' => $key.' ('.$obj->name.')', // label shown in dropdown list
96 'label_name' => $obj->name,
97 );
98 }
99 if (!empty($obj->code_fourn) && !isset($results[$obj->code_fourn])) {
100 $key = $obj->code_fourn;
101 $results[$key] = array(
102 'key' => $key,
103 'value' => $key.' ('.$obj->name.')',
104 'label' => $key.' ('.$obj->name.')',
105 'label_name' => $obj->name,
106 );
107 }
108 }
109 $db->free($resql);
110 } else {
111 dol_syslog("auxaccount.php: error societe query ".$db->lasterror(), LOG_ERR);
112 }
113
114 // --- Users ---
115 $sql2 = "SELECT DISTINCT accountancy_code, lastname, firstname";
116 $sql2 .= " FROM ".$db->prefix()."user";
117 $sql2 .= " WHERE entity IN (".getEntity('user').")";
118 $sql2 .= " AND accountancy_code != ''";
119 $sql2 .= " AND (accountancy_code LIKE '".$db->escape($prefix.$searchkey)."%'";
120 $sql2 .= " OR lastname LIKE '".$db->escape($prefix.$searchkey)."%'";
121 $sql2 .= " OR firstname LIKE '".$db->escape($prefix.$searchkey)."%')";
122 $sql2 .= $db->order("accountancy_code", "ASC");
123 $sql2 .= $db->plimit($limit, 0);
124
125 $resql2 = $db->query($sql2);
126 if ($resql2) {
127 while ($obj = $db->fetch_object($resql2)) {
128 if (!empty($obj->accountancy_code) && !isset($results[$obj->accountancy_code])) {
129 $key = $obj->accountancy_code;
130 $fullname = dolGetFirstLastname($obj->firstname, $obj->lastname);
131 $results[$key] = array(
132 'key' => $key,
133 'value' => $key.' ('.$fullname.')',
134 'label' => $key.' ('.$fullname.')',
135 'label_name' => $fullname,
136 );
137 }
138 }
139 $db->free($resql2);
140 } else {
141 dol_syslog("auxaccount.php: error user query ".$db->lasterror(), LOG_ERR);
142 }
143
144 ksort($results);
145}
146
147// Output JSON — format expected by ajax_autocompleter(): array of {key, value, label}
148top_httphead('application/json');
149echo json_encode(array_values($results));
150
151$db->close();
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.