1<?php
2/* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2004-2012 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2012      Marcos García        <marcosgdf@gmail.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/compta/deplacement/stats/index.php
22 *  \ingroup    deplacement
23 *  \brief      Page for statistics of module trips and expenses
24 */
25
26require '../../../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
28require_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacementstats.class.php';
29
30// Load translation files required by the page
31$langs->loadLangs(array('trips', 'companies'));
32
33$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
34$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
35
36$userid = GETPOST('userid', 'int'); if ($userid < 0) $userid = 0;
37$socid = GETPOST('socid', 'int'); if ($socid < 0) $socid = 0;
38$id = GETPOST('id', 'int');
39
40// Security check
41if ($user->socid > 0)
42{
43	$action = '';
44	$socid = $user->socid;
45}
46if ($user->socid) $socid = $user->socid;
47$result = restrictedArea($user, 'deplacement', $id, '');
48
49// Other security check
50$childids = $user->getAllChildIds();
51$childids[] = $user->id;
52if ($userid > 0)
53{
54	if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous) && !in_array($userid, $childids))
55	{
56		accessforbidden();
57		exit;
58	}
59}
60
61$nowyear = strftime("%Y", dol_now());
62$year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
63//$startyear=$year-2;
64$startyear = $year - 1;
65$endyear = $year;
66
67$mode = GETPOST("mode") ?GETPOST("mode") : 'customer';
68
69
70/*
71 * View
72 */
73
74$form = new Form($db);
75
76
77llxHeader();
78
79$title = $langs->trans("TripsAndExpensesStatistics");
80$dir = $conf->deplacement->dir_temp;
81
82print load_fiche_titre($title, $mesg);
83
84dol_mkdir($dir);
85
86$useridtofilter = $userid; // Filter from parameters
87if (empty($useridtofilter))
88{
89	$useridtofilter = $childids;
90	if (!empty($user->rights->deplacement->readall) || !empty($user->rights->deplacement->lire_tous)) $useridtofilter = 0;
91}
92
93$stats = new DeplacementStats($db, $socid, $useridtofilter);
94
95
96// Build graphic number of object
97// $data = array(array('Lib',val1,val2,val3),...)
98//print "$endyear, $startyear";
99$data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
100//var_dump($data);
101
102$filenamenb = $dir."/tripsexpensesnbinyear-".$year.".png";
103$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesnbinyear-'.$year.'.png';
104
105$px1 = new DolGraph();
106$mesg = $px1->isGraphKo();
107if (!$mesg)
108{
109	$px1->SetData($data);
110	$i = $startyear; $legend = array();
111	while ($i <= $endyear)
112	{
113		$legend[] = $i;
114		$i++;
115	}
116	$px1->SetLegend($legend);
117	$px1->SetMaxValue($px1->GetCeilMaxValue());
118	$px1->SetWidth($WIDTH);
119	$px1->SetHeight($HEIGHT);
120	$px1->SetYLabel($langs->trans("Number"));
121	$px1->SetShading(3);
122	$px1->SetHorizTickIncrement(1);
123	$px1->mode = 'depth';
124	$px1->SetTitle($langs->trans("NumberByMonth"));
125
126	$px1->draw($filenamenb, $fileurlnb);
127}
128
129// Build graphic amount of object
130$data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
131//var_dump($data);
132// $data = array(array('Lib',val1,val2,val3),...)
133
134$filenameamount = $dir."/tripsexpensesamountinyear-".$year.".png";
135$fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesamountinyear-'.$year.'.png';
136
137$px2 = new DolGraph();
138$mesg = $px2->isGraphKo();
139if (!$mesg)
140{
141	$px2->SetData($data);
142	$i = $startyear; $legend = array();
143	while ($i <= $endyear)
144	{
145		$legend[] = $i;
146		$i++;
147	}
148	$px2->SetLegend($legend);
149	$px2->SetMaxValue($px2->GetCeilMaxValue());
150	$px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
151	$px2->SetWidth($WIDTH);
152	$px2->SetHeight($HEIGHT);
153	$px2->SetYLabel($langs->trans("Amount"));
154	$px2->SetShading(3);
155	$px2->SetHorizTickIncrement(1);
156	$px2->mode = 'depth';
157	$px2->SetTitle($langs->trans("AmountTotal"));
158
159	$px2->draw($filenameamount, $fileurlamount);
160}
161
162
163$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
164
165if (!$user->rights->societe->client->voir || $user->socid)
166{
167	$filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
168	if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
169	if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
170} else {
171	$filename_avg = $dir.'/ordersaverage-'.$year.'.png';
172	if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
173	if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
174}
175
176$px3 = new DolGraph();
177$mesg = $px3->isGraphKo();
178if (!$mesg)
179{
180	$px3->SetData($data);
181	$i = $startyear; $legend = array();
182	while ($i <= $endyear)
183	{
184		$legend[] = $i;
185		$i++;
186	}
187	$px3->SetLegend($legend);
188	$px3->SetYLabel($langs->trans("AmountAverage"));
189	$px3->SetMaxValue($px3->GetCeilMaxValue());
190	$px3->SetMinValue($px3->GetFloorMinValue());
191	$px3->SetWidth($WIDTH);
192	$px3->SetHeight($HEIGHT);
193	$px3->SetShading(3);
194	$px3->SetHorizTickIncrement(1);
195	$px3->mode = 'depth';
196	$px3->SetTitle($langs->trans("AmountAverage"));
197
198	$px3->draw($filename_avg, $fileurl_avg);
199}
200
201
202// Show array
203$data = $stats->getAllByYear();
204$arrayyears = array();
205foreach ($data as $val) {
206	$arrayyears[$val['year']] = $val['year'];
207}
208if (!count($arrayyears)) $arrayyears[$nowyear] = $nowyear;
209
210
211$h = 0;
212$head = array();
213$head[$h][0] = DOL_URL_ROOT.'/compta/deplacement/stats/index.php';
214$head[$h][1] = $langs->trans("ByMonthYear");
215$head[$h][2] = 'byyear';
216$h++;
217
218complete_head_from_modules($conf, $langs, null, $head, $h, 'trip_stats');
219
220print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
221
222
223print '<div class="fichecenter"><div class="fichethirdleft">';
224
225
226// Show filter box
227print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
228print '<input type="hidden" name="token" value="'.newToken().'">';
229print '<input type="hidden" name="mode" value="'.$mode.'">';
230
231print '<table class="border centpercent">';
232print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
233// Company
234print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
235$filter = '';
236print $form->select_company($socid, 'socid', $filter, 1, 1, 0, array(), 0, '', 'style="width: 95%"');
237print '</td></tr>';
238// User
239print '<tr><td>'.$langs->trans("User").'</td><td>';
240$include = '';
241if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous)) $include = 'hierarchy';
242print $form->select_dolusers($userid, 'userid', 1, '', 0, $include, '', 0, 0, 0, '', 0, '', 'maxwidth300');
243print '</td></tr>';
244// Year
245print '<tr><td>'.$langs->trans("Year").'</td><td>';
246if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year;
247arsort($arrayyears);
248print $form->selectarray('year', $arrayyears, $year, 0);
249print '</td></tr>';
250print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
251print '</table>';
252print '</form>';
253print '<br><br>';
254
255print '<div class="div-table-responsive-no-min">';
256print '<table class="border centpercent">';
257print '<tr height="24">';
258print '<td class="center">'.$langs->trans("Year").'</td>';
259print '<td class="center">'.$langs->trans("Number").'</td>';
260print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
261print '<td class="center">'.$langs->trans("AmountAverage").'</td>';
262print '</tr>';
263
264$oldyear = 0;
265foreach ($data as $val)
266{
267	$year = $val['year'];
268	while ($year && $oldyear > $year + 1)
269	{	// If we have empty year
270		$oldyear--;
271		print '<tr height="24">';
272		print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.'">'.$oldyear.'</a></td>';
273		print '<td class="right">0</td>';
274		print '<td class="right">0</td>';
275		print '<td class="right">0</td>';
276		print '</tr>';
277	}
278	print '<tr height="24">';
279	print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.'">'.$year.'</a></td>';
280	print '<td class="right">'.$val['nb'].'</td>';
281	print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
282	print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
283	print '</tr>';
284	$oldyear = $year;
285}
286
287print '</table>';
288print '</div>';
289
290print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
291
292
293// Show graphs
294print '<table class="border centpercent"><tr class="pair nohover"><td align="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
308
309print dol_get_fiche_end();
310
311// End of page
312llxFooter();
313$db->close();
314