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