dolibarr 24.0.0-beta
order.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4 * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2025-2026 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
37{
38 global $db, $langs, $conf, $user;
39
40 if (isModEnabled("shipping")) {
41 $langs->load("sendings");
42 }
43 $langs->load("orders");
44
45 $h = 0;
46 $head = array();
47
48 if (isModEnabled('order') && $user->hasRight('commande', 'lire')) {
49 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/commande/card.php', ['id' => $object->id]);
50 $head[$h][1] = $langs->trans("CustomerOrder");
51 $head[$h][2] = 'order';
52 $h++;
53 }
54
55 if (!getDolGlobalString('MAIN_DISABLE_CONTACTS_TAB')) {
56 $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
57 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/commande/contact.php', ['id' => $object->id]);
58 $head[$h][1] = $langs->trans('ContactsAddresses');
59 if ($nbContact > 0) {
60 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
61 }
62 $head[$h][2] = 'contact';
63 $h++;
64 }
65
66 if ((getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && $user->hasRight('expedition', 'lire'))
67 || (getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && $user->hasRight('expedition', 'delivery', 'lire'))) {
68 $nbShipments = $object->getNbOfShipments();
69 $nbReceiption = 0;
70 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/expedition/shipment.php', ['id' => $object->id]);
71 $text = '';
72 if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
73 $text .= $langs->trans("Shipments");
74 }
75 if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
76 $text .= ' - ';
77 }
78 if (getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
79 $text .= $langs->trans("Receivings");
80 }
81 if ($nbShipments > 0 || $nbReceiption > 0) {
82 $text .= '<span class="badge marginleftonlyshort">'.($nbShipments ? $nbShipments : 0);
83 }
84 if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && ($nbShipments > 0 || $nbReceiption > 0)) {
85 $text .= ' - ';
86 }
87 if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && ($nbShipments > 0 || $nbReceiption > 0)) {
88 $text .= ($nbReceiption ? $nbReceiption : 0);
89 }
90 if ($nbShipments > 0 || $nbReceiption > 0) {
91 $text .= '</span>';
92 }
93 $head[$h][1] = $text;
94 $head[$h][2] = 'shipping';
95 $h++;
96 }
97
98 // Show more tabs from modules
99 // Entries must be declared in modules descriptor with line
100 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
101 // $this->tabs = array('entity:-tabname); to remove a tab
102 complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'add', 'core');
103
104 if (!getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
105 $nbNote = 0;
106 if (!empty($object->note_private)) {
107 $nbNote++;
108 }
109 if (!empty($object->note_public)) {
110 $nbNote++;
111 }
112 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/commande/note.php', ['id' => $object->id]);
113 $head[$h][1] = $langs->trans('Notes');
114 if ($nbNote > 0) {
115 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
116 }
117 $head[$h][2] = 'note';
118 $h++;
119 }
120
121 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
122 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
123 $upload_dir = $conf->commande->multidir_output[$object->entity ?? $conf->entity]."/".dol_sanitizeFileName($object->ref);
124 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
125 $nbLinks = Link::count($db, $object->element, $object->id);
126 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/commande/document.php', ['id' => $object->id]);
127 $head[$h][1] = $langs->trans('Documents');
128 if (($nbFiles + $nbLinks) > 0) {
129 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
130 }
131 $head[$h][2] = 'documents';
132 $h++;
133
134
135 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/commande/agenda.php', ['id' => $object->id]);
136 $head[$h][1] = $langs->trans("Events");
137 if (isModEnabled('agenda') && ($user->hasRight('agenda', 'myactions', 'read') || $user->hasRight('agenda', 'allactions', 'read'))) {
138 $nbEvent = 0;
139 // Enable caching of thirdparty count actioncomm
140 require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
141 $cachekey = 'count_events_propal_'.$object->id;
142 $dataretrieved = dol_getcache($cachekey);
143 if (!is_null($dataretrieved)) {
144 $nbEvent = $dataretrieved;
145 } else {
146 $sql = "SELECT COUNT(id) as nb";
147 $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
148 $sql .= " WHERE fk_element = ".((int) $object->id);
149 $sql .= " AND elementtype = 'order'";
150 $resql = $db->query($sql);
151 if ($resql) {
152 $obj = $db->fetch_object($resql);
153 $nbEvent = $obj->nb;
154 } else {
155 dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
156 }
157 dol_setcache($cachekey, $nbEvent, 120); // If setting cache fails, this is not a problem, so we do not test result.
158 }
159
160 $head[$h][1] .= '/';
161 $head[$h][1] .= $langs->trans("Agenda");
162 if ($nbEvent > 0) {
163 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbEvent.'</span>';
164 }
165 }
166 $head[$h][2] = 'agenda';
167 $h++;
168
169 complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'add', 'external');
170
171 complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'remove');
172
173 return $head;
174}
175
182{
183 global $langs, $conf, $extrafields;
184
185 $extrafields->fetch_name_optionals_label('commande');
186 $extrafields->fetch_name_optionals_label('commandedet');
187
188 $h = 0;
189 $head = array();
190
191 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/admin/order.php');
192 $head[$h][1] = $langs->trans("Miscellaneous");
193 $head[$h][2] = 'general';
194 $h++;
195
196 complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin');
197
198 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/admin/order_extrafields.php');
199 $head[$h][1] = $langs->trans("ExtraFields");
200 $nbExtrafields = $extrafields->attributes['commande']['count'];
201 if ($nbExtrafields > 0) {
202 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
203 }
204 $head[$h][2] = 'attributes';
205 $h++;
206
207 $head[$h][0] = dolBuildUrl(DOL_URL_ROOT . '/admin/orderdet_extrafields.php');
208 $head[$h][1] = $langs->trans("ExtraFieldsLines");
209 $nbExtrafields = $extrafields->attributes['commandedet']['count'];
210 if ($nbExtrafields > 0) {
211 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
212 }
213 $head[$h][2] = 'attributeslines';
214 $h++;
215
216 complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin', 'remove');
217
218 return $head;
219}
220
221
222
229function getCustomerOrderPieChart($socid = 0)
230{
231 global $conf, $db, $langs, $user, $hookmanager;
232
233 $result = '';
234
235 if (!isModEnabled('order') || !$user->hasRight('commande', 'lire')) {
236 return '';
237 }
238
239 $commandestatic = new Commande($db);
240
241 /*
242 * Statistics
243 */
244 if ($user->socid > 0) $socid = $user->socid;
245 $sql = "SELECT count(c.rowid) as nb, c.fk_statut as status";
246 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
247 $sql .= ", ".MAIN_DB_PREFIX."commande as c";
248 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
249 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
250 }
251 $sql .= " WHERE c.fk_soc = s.rowid";
252 $sql .= " AND c.entity IN (".getEntity($commandestatic->element).")";
253 if ($user->socid) {
254 $sql .= ' AND c.fk_soc = '.((int) $user->socid);
255 }
256 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
257 $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
258 }
259 // Add where from hooks
260 $parameters = array('socid' => $user->socid);
261 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $commandestatic); // Note that $action and $object may have been modified by hook
262 $sql .= $hookmanager->resPrint;
263 $sql .= " GROUP BY c.fk_statut";
264
265 $resql = $db->query($sql);
266 if ($resql) {
267 $num = $db->num_rows($resql);
268 $i = 0;
269
270 $total = 0;
271 $totalinprocess = 0;
272 $dataseries = array();
273 $colorseries = array();
274 $vals = array();
275 // -1=Canceled, 0=Draft, 1=Validated, 2=Accepted/On process, 3=Closed (Sent/Received, billed or not)
276 while ($i < $num) {
277 $row = $db->fetch_row($resql);
278 if ($row) {
279 if (!isset($vals[$row[1]])) {
280 $vals[$row[1]] = 0;
281 }
282 $vals[$row[1]] += $row[0];
283 $totalinprocess += $row[0];
284 $total += $row[0];
285 }
286 $i++;
287 }
288 $db->free($resql);
289
290 global $badgeStatus0, $badgeStatus1, $badgeStatus4, $badgeStatus6, $badgeStatus9;
291 include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
292
293 $result = '<div class="div-table-responsive-no-min">';
294 $result .= '<table class="noborder nohover centpercent">';
295 $result .= '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("CustomersOrders").'</th></tr>'."\n";
296 $listofstatus = array(0, 1, 2, 3, -1);
297 foreach ($listofstatus as $status) {
298 $dataseries[] = array($commandestatic->LibStatut($status, 0, 1, 1), (isset($vals[$status]) ? (int) $vals[$status] : 0));
299 if ($status == Commande::STATUS_DRAFT) {
300 $colorseries[$status] = '-'.$badgeStatus0;
301 }
302 if ($status == Commande::STATUS_VALIDATED) {
303 $colorseries[$status] = $badgeStatus1;
304 }
305 if ($status == Commande::STATUS_SHIPMENTONPROCESS) {
306 $colorseries[$status] = $badgeStatus4;
307 }
308 if ($status == Commande::STATUS_CLOSED) {
309 $colorseries[$status] = $badgeStatus6;
310 }
311 if ($status == Commande::STATUS_CANCELED) {
312 $colorseries[$status] = $badgeStatus9;
313 }
314
315 if (empty($conf->use_javascript_ajax)) {
316 $result .= '<tr class="oddeven">';
317 $result .= '<td>'.$commandestatic->LibStatut($status, 0, 0, 1).'</td>';
318 $result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).' ';
319 $result .= $commandestatic->LibStatut($status, 0, 3, 1);
320 $result .= '</a></td>';
321 $result .= "</tr>\n";
322 }
323 }
324 if (!empty($conf->use_javascript_ajax)) {
325 $result .= '<tr class="oddeven"><td align="center" colspan="2">';
326
327 include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
328 $dolgraph = new DolGraph();
329 $dolgraph->SetData($dataseries);
330 $dolgraph->SetDataColor(array_values($colorseries));
331 $dolgraph->setShowLegend(2);
332 $dolgraph->setShowPercent(1);
333 $dolgraph->SetType(array('pie'));
334 $dolgraph->setHeight('150');
335 $dolgraph->setWidth('300');
336 $dolgraph->draw('idgraphstatus');
337 $result .= $dolgraph->show($total ? 0 : 1);
338
339 $result .= '</td></tr>';
340 }
341
342 //if ($totalinprocess != $total)
343 $result .= '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td class="right">'.$total.'</td></tr>';
344 $result .= "</table></div><br>";
345 } else {
347 }
348
349 return $result;
350}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage customers orders.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
const STATUS_CLOSED
Closed (Sent, billed or not)
const STATUS_CANCELED
Canceled status.
const STATUS_DRAFT
Draft status.
const STATUS_VALIDATED
Validated status.
Class to build graphs.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:64
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
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.
dol_setcache($memoryid, $data, $expire=0, $filecache=0, $replace=0)
Save data into a memory area shared by all users, all sessions on server.
dol_getcache($memoryid, $filecache=0)
Read a memory area shared by all users, all sessions on server.
order_admin_prepare_head()
Return array head with list of tabs to view object information.
getCustomerOrderPieChart($socid=0)
Return a HTML table that contains a pie chart of sales orders.
commande_prepare_head(Commande $object)
Prepare array with list of tabs.
Definition order.lib.php:36