1<?php
2/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2016 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin        <regis.houssin@inodbox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/**
21 *     \file       htdocs/expedition/stats/index.php
22 *     \ingroup    expedition
23 *     \brief      Page with shipment statistics
24 */
25
26require '../../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
28require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionstats.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
30
31$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
32$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
33
34$userid = GETPOST('userid', 'int');
35$socid = GETPOST('socid', 'int');
36// Security check
37if ($user->socid > 0)
38{
39	$action = '';
40	$socid = $user->socid;
41}
42
43$nowyear = strftime("%Y", dol_now());
44$year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
45//$startyear=$year-2;
46$startyear = $year - 1;
47$endyear = $year;
48
49// Load translation files required by the page
50$langs->loadLangs(array('sendings', 'other', 'companies'));
51
52
53/*
54 * View
55 */
56
57$form = new Form($db);
58
59llxHeader();
60
61print load_fiche_titre($langs->trans("StatisticsOfSendings"), '', 'dolly');
62
63
64dol_mkdir($dir);
65
66$stats = new ExpeditionStats($db, $socid, '', ($userid > 0 ? $userid : 0));
67
68// Build graphic number of object
69$data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
70//var_dump($data);exit;
71// $data = array(array('Lib',val1,val2,val3),...)
72
73
74if (!$user->rights->societe->client->voir || $user->socid)
75{
76	$filenamenb = $dir.'/shipmentsnbinyear-'.$user->id.'-'.$year.'.png';
77} else {
78	$filenamenb = $dir.'/shipmentsnbinyear-'.$year.'.png';
79}
80
81$px1 = new DolGraph();
82$mesg = $px1->isGraphKo();
83if (!$mesg)
84{
85	$px1->SetData($data);
86	$i = $startyear; $legend = array();
87	while ($i <= $endyear)
88	{
89		$legend[] = $i;
90		$i++;
91	}
92	$px1->SetLegend($legend);
93	$px1->SetMaxValue($px1->GetCeilMaxValue());
94	$px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
95	$px1->SetWidth($WIDTH);
96	$px1->SetHeight($HEIGHT);
97	$px1->SetYLabel($langs->trans("NbOfSendings"));
98	$px1->SetShading(3);
99	$px1->SetHorizTickIncrement(1);
100	$px1->mode = 'depth';
101	$px1->SetTitle($langs->trans("NumberOfShipmentsByMonth"));
102
103	$px1->draw($filenamenb, $fileurlnb);
104}
105
106// Build graphic amount of object
107/*
108$data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
109//var_dump($data);
110// $data = array(array('Lib',val1,val2,val3),...)
111
112if (!$user->rights->societe->client->voir || $user->socid)
113{
114    $filenameamount = $dir.'/shipmentsamountinyear-'.$user->id.'-'.$year.'.png';
115}
116else
117{
118    $filenameamount = $dir.'/shipmentsamountinyear-'.$year.'.png';
119}
120
121$px2 = new DolGraph();
122$mesg = $px2->isGraphKo();
123if (! $mesg)
124{
125    $px2->SetData($data);
126    $i=$startyear;$legend=array();
127    while ($i <= $endyear)
128    {
129        $legend[]=$i;
130        $i++;
131    }
132    $px2->SetLegend($legend);
133    $px2->SetMaxValue($px2->GetCeilMaxValue());
134    $px2->SetMinValue(min(0,$px2->GetFloorMinValue()));
135    $px2->SetWidth($WIDTH);
136    $px2->SetHeight($HEIGHT);
137    $px2->SetYLabel($langs->trans("AmountOfShipments"));
138    $px2->SetShading(3);
139    $px2->SetHorizTickIncrement(1);
140    $px2->mode='depth';
141    $px2->SetTitle($langs->trans("AmountOfShipmentsByMonthHT"));
142
143    $px2->draw($filenameamount,$fileurlamount);
144}
145*/
146
147/*
148$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
149
150if (!$user->rights->societe->client->voir || $user->socid)
151{
152    $filename_avg = $dir.'/shipmentsaverage-'.$user->id.'-'.$year.'.png';
153}
154else
155{
156    $filename_avg = $dir.'/shipmentsaverage-'.$year.'.png';
157}
158
159$px3 = new DolGraph();
160$mesg = $px3->isGraphKo();
161if (! $mesg)
162{
163    $px3->SetData($data);
164    $i=$startyear;$legend=array();
165    while ($i <= $endyear)
166    {
167        $legend[]=$i;
168        $i++;
169    }
170    $px3->SetLegend($legend);
171    $px3->SetYLabel($langs->trans("AmountAverage"));
172    $px3->SetMaxValue($px3->GetCeilMaxValue());
173    $px3->SetMinValue($px3->GetFloorMinValue());
174    $px3->SetWidth($WIDTH);
175    $px3->SetHeight($HEIGHT);
176    $px3->SetShading(3);
177    $px3->SetHorizTickIncrement(1);
178    $px3->mode='depth';
179    $px3->SetTitle($langs->trans("AmountAverage"));
180
181    $px3->draw($filename_avg,$fileurl_avg);
182}
183*/
184
185
186// Show array
187$data = $stats->getAllByYear();
188$arrayyears = array();
189foreach ($data as $val) {
190	if (!empty($val['year'])) {
191		$arrayyears[$val['year']] = $val['year'];
192	}
193}
194if (!count($arrayyears)) $arrayyears[$nowyear] = $nowyear;
195
196$h = 0;
197$head = array();
198$head[$h][0] = DOL_URL_ROOT.'/expedition/stats/index.php';
199$head[$h][1] = $langs->trans("ByMonthYear");
200$head[$h][2] = 'byyear';
201$h++;
202
203$type = 'shipment_stats';
204
205complete_head_from_modules($conf, $langs, null, $head, $h, $type);
206
207print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
208
209
210print '<div class="fichecenter"><div class="fichethirdleft">';
211
212
213//if (empty($socid))
214//{
215	// Show filter box
216	print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
217	print '<input type="hidden" name="token" value="'.newToken().'">';
218
219	print '<table class="noborder centpercent">';
220	print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
221	// Company
222	print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
223	print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, '', 'style="width: 95%"');
224	print '</td></tr>';
225	// User
226	print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
227	print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
228	print '</td></tr>';
229	// Year
230	print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
231	if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year;
232	if (!in_array($nowyear, $arrayyears)) $arrayyears[$nowyear] = $nowyear;
233	arsort($arrayyears);
234	print $form->selectarray('year', $arrayyears, $year, 0);
235	print '</td></tr>';
236	print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
237	print '</table>';
238	print '</form>';
239	print '<br><br>';
240//}
241
242
243print '<div class="div-table-responsive-no-min">';
244print '<table class="noborder centpercent">';
245print '<tr class="liste_titre" height="24">';
246print '<td class="center">'.$langs->trans("Year").'</td>';
247print '<td class="right">'.$langs->trans("NbOfSendings").'</td>';
248/*print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
249print '<td class="center">'.$langs->trans("AmountAverage").'</td>';*/
250print '</tr>';
251
252$oldyear = 0;
253foreach ($data as $val)
254{
255	$year = $val['year'];
256	while (!empty($year) && $oldyear > $year + 1)
257	{ // If we have empty year
258		$oldyear--;
259
260
261		print '<tr class="oddeven" height="24">';
262		print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'">'.$oldyear.'</a></td>';
263
264		print '<td class="right">0</td>';
265		/*print '<td class="right">0</td>';
266		print '<td class="right">0</td>';*/
267		print '</tr>';
268	}
269
270	print '<tr class="oddeven" height="24">';
271	print '<td class="center">';
272	if ($year) {
273		print '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'">'.$year.'</a>';
274	} else {
275		// Technical error that should not happen
276		print 'Error: validation date of shipment is not defined. This looks strange because shipment is validated. Try to run /install/repair.php?standard=confirmed';
277	}
278	print '</td>';
279	print '<td class="right">'.$val['nb'].'</td>';
280	/*print '<td class="right">'.price(price2num($val['total'],'MT'),1).'</td>';
281	print '<td class="right">'.price(price2num($val['avg'],'MT'),1).'</td>';*/
282	print '</tr>';
283	$oldyear = $year;
284}
285
286print '</table>';
287print '</div>';
288
289
290print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
291
292
293// Show graphs
294print '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
295if ($mesg) { print $mesg; } else {
296	print $px1->show();
297	print "<br>\n";
298	/*print $px2->show();
299    print "<br>\n";
300    print $px3->show();*/
301}
302print '</td></tr></table>';
303
304
305print '</div></div></div>';
306print '<div style="clear:both"></div>';
307
308print dol_get_fiche_end();
309
310
311
312// TODO USe code similar to commande/stats/index.php instead of this one.
313/*
314print '<table class="border centpercent">';
315print '<tr><td class="center">'.$langs->trans("Year").'</td>';
316print '<td width="40%" class="center">'.$langs->trans("NbOfSendings").'</td></tr>';
317
318$sql = "SELECT count(*) as nb, date_format(date_expedition,'%Y') as dm";
319$sql.= " FROM ".MAIN_DB_PREFIX."expedition";
320$sql.= " WHERE fk_statut > 0";
321$sql.= " AND entity = ".$conf->entity;
322$sql.= " GROUP BY dm DESC";
323
324$resql=$db->query($sql);
325if ($resql)
326{
327    $num = $db->num_rows($resql);
328    $i = 0;
329    while ($i < $num)
330    {
331        $row = $db->fetch_row($resql);
332        $nbproduct = $row[0];
333        $year = $row[1];
334        print "<tr>";
335        print '<td class="center"><a href="month.php?year='.$year.'">'.$year.'</a></td><td class="center">'.$nbproduct.'</td></tr>';
336        $i++;
337    }
338}
339$db->free($resql);
340
341print '</table>';
342*/
343
344print '<br>';
345print '<i class="opacitymedium">'.$langs->trans("StatsOnShipmentsOnlyValidated").'</i>';
346
347// End of page
348llxFooter();
349$db->close();
350