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