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
22/**
23 *  \file       htdocs/core/lib/order.lib.php
24 *  \brief      Ensemble de fonctions de base pour le module commande
25 *  \ingroup    commande
26 */
27
28/**
29 * Prepare array with list of tabs
30 *
31 * @param   Commande	$object		Object related to tabs
32 * @return  array				Array of tabs to show
33 */
34function commande_prepare_head(Commande $object)
35{
36	global $db, $langs, $conf, $user;
37	if (!empty($conf->expedition->enabled)) {
38		$langs->load("sendings");
39	}
40	$langs->load("orders");
41
42	$h = 0;
43	$head = array();
44
45	if (!empty($conf->commande->enabled) && $user->rights->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 (($conf->expedition_bon->enabled && $user->rights->expedition->lire)
64	|| ($conf->delivery_note->enabled && $user->rights->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 ($conf->expedition_bon->enabled) {
70			$text .= $langs->trans("Shipments");
71		}
72		if ($conf->expedition_bon->enabled && $conf->delivery_note->enabled) {
73			$text .= ' - ';
74		}
75		if ($conf->delivery_note->enabled) {
76			$text .= $langs->trans("Receivings");
77		}
78		if ($nbShipments > 0 || $nbReceiption > 0) {
79			$text .= '<span class="badge marginleftonlyshort">'.($nbShipments ? $nbShipments : 0);
80		}
81		if ($conf->expedition_bon->enabled && $conf->delivery_note->enabled && ($nbShipments > 0 || $nbReceiption > 0)) {
82			$text .= ' - ';
83		}
84		if ($conf->expedition_bon->enabled && $conf->delivery_note->enabled && ($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');
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	$head[$h][0] = DOL_URL_ROOT.'/commande/info.php?id='.$object->id;
132	$head[$h][1] = $langs->trans("Info");
133	$head[$h][2] = 'info';
134	$h++;
135
136	complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'remove');
137
138	return $head;
139}
140
141/**
142 *  Return array head with list of tabs to view object informations.
143 *
144 *  @return	array   	    		    head array with tabs
145 */
146function order_admin_prepare_head()
147{
148	global $langs, $conf, $user;
149
150	$h = 0;
151	$head = array();
152
153	$head[$h][0] = DOL_URL_ROOT.'/admin/commande.php';
154	$head[$h][1] = $langs->trans("Miscellaneous");
155	$head[$h][2] = 'general';
156	$h++;
157
158	complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin');
159
160	$head[$h][0] = DOL_URL_ROOT.'/admin/order_extrafields.php';
161	$head[$h][1] = $langs->trans("ExtraFields");
162	$head[$h][2] = 'attributes';
163	$h++;
164
165	$head[$h][0] = DOL_URL_ROOT.'/admin/orderdet_extrafields.php';
166	$head[$h][1] = $langs->trans("ExtraFieldsLines");
167	$head[$h][2] = 'attributeslines';
168	$h++;
169
170	complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin', 'remove');
171
172	return $head;
173}
174
175
176
177/**
178 * Return a HTML table that contains a pie chart of customer orders
179 *
180 * @param	int		$socid		(Optional) Show only results from the customer with this id
181 * @return	string				A HTML table that contains a pie chart of customer invoices
182 */
183function getCustomerOrderPieChart($socid = 0)
184{
185	global $conf, $db, $langs, $user;
186
187	$result = '';
188
189	if (empty($conf->commande->enabled) || empty($user->rights->commande->lire)) {
190		return '';
191	}
192
193	$commandestatic = new Commande($db);
194
195	/*
196	 * Statistics
197	 */
198
199	$sql = "SELECT count(c.rowid) as nb, c.fk_statut as status";
200	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
201	$sql .= ", ".MAIN_DB_PREFIX."commande as c";
202	if (!$user->rights->societe->client->voir && !$socid) {
203		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
204	}
205	$sql .= " WHERE c.fk_soc = s.rowid";
206	$sql .= " AND c.entity IN (".getEntity('societe').")";
207	if ($user->socid) {
208		$sql .= ' AND c.fk_soc = '.$user->socid;
209	}
210	if (!$user->rights->societe->client->voir && !$socid) {
211		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
212	}
213	$sql .= " GROUP BY c.fk_statut";
214
215	$resql = $db->query($sql);
216	if ($resql) {
217		$num = $db->num_rows($resql);
218		$i = 0;
219
220		$total = 0;
221		$totalinprocess = 0;
222		$dataseries = array();
223		$colorseries = array();
224		$vals = array();
225		// -1=Canceled, 0=Draft, 1=Validated, 2=Accepted/On process, 3=Closed (Sent/Received, billed or not)
226		while ($i < $num) {
227			$row = $db->fetch_row($resql);
228			if ($row) {
229				//if ($row[1]!=-1 && ($row[1]!=3 || $row[2]!=1))
230				{
231				if (!isset($vals[$row[1]])) {
232					$vals[$row[1]] = 0;
233				}
234					$vals[$row[1]] += $row[0];
235					$totalinprocess += $row[0];
236				}
237				$total += $row[0];
238			}
239			$i++;
240		}
241		$db->free($resql);
242
243		include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
244
245		$result = '<div class="div-table-responsive-no-min">';
246		$result .= '<table class="noborder nohover centpercent">';
247		$result .= '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("CustomersOrders").'</th></tr>'."\n";
248		$listofstatus = array(0, 1, 2, 3, -1);
249		foreach ($listofstatus as $status) {
250			$dataseries[] = array($commandestatic->LibStatut($status, 0, 1, 1), (isset($vals[$status]) ? (int) $vals[$status] : 0));
251			if ($status == Commande::STATUS_DRAFT) {
252				$colorseries[$status] = '-'.$badgeStatus0;
253			}
254			if ($status == Commande::STATUS_VALIDATED) {
255				$colorseries[$status] = $badgeStatus1;
256			}
257			if ($status == Commande::STATUS_SHIPMENTONPROCESS) {
258				$colorseries[$status] = $badgeStatus4;
259			}
260			if ($status == Commande::STATUS_CLOSED && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT)) {
261				$colorseries[$status] = $badgeStatus6;
262			}
263			if ($status == Commande::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
264				$colorseries[$status] = $badgeStatus6;
265			}
266			if ($status == Commande::STATUS_CANCELED) {
267				$colorseries[$status] = $badgeStatus9;
268			}
269
270			if (empty($conf->use_javascript_ajax)) {
271				$result .= '<tr class="oddeven">';
272				$result .= '<td>'.$commandestatic->LibStatut($status, 0, 0, 1).'</td>';
273				$result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).' ';
274				$result .= $commandestatic->LibStatut($status, 0, 3, 1);
275				$result .= '</a></td>';
276				$result .= "</tr>\n";
277			}
278		}
279		if ($conf->use_javascript_ajax) {
280			$result .= '<tr class="impair"><td align="center" colspan="2">';
281
282			include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
283			$dolgraph = new DolGraph();
284			$dolgraph->SetData($dataseries);
285			$dolgraph->SetDataColor(array_values($colorseries));
286			$dolgraph->setShowLegend(2);
287			$dolgraph->setShowPercent(1);
288			$dolgraph->SetType(array('pie'));
289			$dolgraph->setHeight('150');
290			$dolgraph->setWidth('300');
291			$dolgraph->draw('idgraphstatus');
292			$result .= $dolgraph->show($total ? 0 : 1);
293
294			$result .= '</td></tr>';
295		}
296
297		//if ($totalinprocess != $total)
298		$result .= '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td class="right">'.$total.'</td></tr>';
299		$result .= "</table></div><br>";
300	} else {
301		dol_print_error($db);
302	}
303
304	return $result;
305}
306