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