1<?php
2/* Copyright (C) 2016 Laurent Destailleur  <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18/**
19 *	    \file       htdocs/fichinter/stats/index.php
20 *      \ingroup    fichinter
21 *		\brief      Page with interventions statistics
22 */
23
24require '../../main.inc.php';
25require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
26require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinterstats.class.php';
27require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
28
29$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
30$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
31
32$mode = 'customer';
33if (!$user->rights->ficheinter->lire) {
34	accessforbidden();
35}
36
37$userid = GETPOST('userid', 'int');
38$socid = GETPOST('socid', 'int');
39// Security check
40if ($user->socid > 0) {
41	$action = '';
42	$socid = $user->socid;
43}
44
45$nowyear = strftime("%Y", dol_now());
46$year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
47$startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
48$endyear = $year;
49
50$object_status = GETPOST('object_status', 'intcomma');
51
52// Load translation files required by the page
53$langs->loadLangs(array('interventions', 'companies', 'other', 'suppliers'));
54
55
56/*
57 * View
58 */
59
60$form = new Form($db);
61$objectstatic = new FichInter($db);
62
63$title = $langs->trans("InterventionStatistics");
64$dir = $conf->ficheinter->dir_temp;
65
66llxHeader('', $title);
67
68print load_fiche_titre($title, '', 'intervention');
69
70dol_mkdir($dir);
71
72$stats = new FichinterStats($db, $socid, $mode, ($userid > 0 ? $userid : 0));
73if ($object_status != '' && $object_status > -1) {
74	$stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
75}
76
77// Build graphic number of object
78$data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
79// $data = array(array('Lib',val1,val2,val3),...)
80
81
82if (!$user->rights->societe->client->voir || $user->socid) {
83	$filenamenb = $dir.'/interventionsnbinyear-'.$user->id.'-'.$year.'.png';
84	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsnbinyear-'.$user->id.'-'.$year.'.png';
85} else {
86	$filenamenb = $dir.'/interventionsnbinyear-'.$year.'.png';
87	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsnbinyear-'.$year.'.png';
88}
89
90$px1 = new DolGraph();
91$mesg = $px1->isGraphKo();
92if (!$mesg) {
93	$px1->SetData($data);
94	$i = $startyear; $legend = array();
95	while ($i <= $endyear) {
96		$legend[] = $i;
97		$i++;
98	}
99	$px1->SetLegend($legend);
100	$px1->SetMaxValue($px1->GetCeilMaxValue());
101	$px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
102	$px1->SetWidth($WIDTH);
103	$px1->SetHeight($HEIGHT);
104	$px1->SetYLabel($langs->trans("NbOfIntervention"));
105	$px1->SetShading(3);
106	$px1->SetHorizTickIncrement(1);
107	$px1->mode = 'depth';
108	$px1->SetTitle($langs->trans("NumberOfInterventionsByMonth"));
109
110	$px1->draw($filenamenb, $fileurlnb);
111}
112
113// Build graphic amount of object
114$data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
115// $data = array(array('Lib',val1,val2,val3),...)
116
117if (!$user->rights->societe->client->voir || $user->socid) {
118	$filenameamount = $dir.'/interventionsamountinyear-'.$user->id.'-'.$year.'.png';
119	$fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsamountinyear-'.$user->id.'-'.$year.'.png';
120} else {
121	$filenameamount = $dir.'/interventionsamountinyear-'.$year.'.png';
122	$fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsamountinyear-'.$year.'.png';
123}
124
125$px2 = new DolGraph();
126$mesg = $px2->isGraphKo();
127if (!$mesg) {
128	$px2->SetData($data);
129	$i = $startyear; $legend = array();
130	while ($i <= $endyear) {
131		$legend[] = $i;
132		$i++;
133	}
134	$px2->SetLegend($legend);
135	$px2->SetMaxValue($px2->GetCeilMaxValue());
136	$px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
137	$px2->SetWidth($WIDTH);
138	$px2->SetHeight($HEIGHT);
139	$px2->SetYLabel($langs->trans("AmountOfinterventions"));
140	$px2->SetShading(3);
141	$px2->SetHorizTickIncrement(1);
142	$px2->mode = 'depth';
143	$px2->SetTitle($langs->trans("AmountOfinterventionsByMonthHT"));
144
145	$px2->draw($filenameamount, $fileurlamount);
146}
147
148
149$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
150
151if (!$user->rights->societe->client->voir || $user->socid) {
152	$filename_avg = $dir.'/interventionsaverage-'.$user->id.'-'.$year.'.png';
153	$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsaverage-'.$user->id.'-'.$year.'.png';
154} else {
155	$filename_avg = $dir.'/interventionsaverage-'.$year.'.png';
156	$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsaverage-'.$year.'.png';
157}
158
159$px3 = new DolGraph();
160$mesg = $px3->isGraphKo();
161if (!$mesg) {
162	$px3->SetData($data);
163	$i = $startyear; $legend = array();
164	while ($i <= $endyear) {
165		$legend[] = $i;
166		$i++;
167	}
168	$px3->SetLegend($legend);
169	$px3->SetYLabel($langs->trans("AmountAverage"));
170	$px3->SetMaxValue($px3->GetCeilMaxValue());
171	$px3->SetMinValue($px3->GetFloorMinValue());
172	$px3->SetWidth($WIDTH);
173	$px3->SetHeight($HEIGHT);
174	$px3->SetShading(3);
175	$px3->SetHorizTickIncrement(1);
176	$px3->mode = 'depth';
177	$px3->SetTitle($langs->trans("AmountAverage"));
178
179	$px3->draw($filename_avg, $fileurl_avg);
180}
181
182
183
184// Show array
185$data = $stats->getAllByYear();
186$arrayyears = array();
187foreach ($data as $val) {
188	if (!empty($val['year'])) {
189		$arrayyears[$val['year']] = $val['year'];
190	}
191}
192if (!count($arrayyears)) {
193	$arrayyears[$nowyear] = $nowyear;
194}
195
196$h = 0;
197$head = array();
198$head[$h][0] = DOL_URL_ROOT.'/fichinter/stats/index.php';
199$head[$h][1] = $langs->trans("ByMonthYear");
200$head[$h][2] = 'byyear';
201$h++;
202
203$type = 'fichinter_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// Show filter box
214print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
215print '<input type="hidden" name="token" value="'.newToken().'">';
216print '<input type="hidden" name="mode" value="'.$mode.'">';
217
218print '<table class="noborder centpercent">';
219print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
220// Company
221print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
222$filter = 's.client IN (1,2,3)';
223print img_picto('', 'company', 'class="pictofixedwidth"');
224print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
225print '</td></tr>';
226// User
227print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
228print img_picto('', 'user', 'class="pictofixedwidth"');
229print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
230// Status
231print '<tr><td class="left">'.$langs->trans("Status").'</td><td class="left">';
232$tmp = $objectstatic->LibStatut(0); // To force load of $this->statuts_short
233$liststatus = $objectstatic->statuts_short;
234if (empty($conf->global->FICHINTER_CLASSIFY_BILLED)) {
235	unset($liststatus[2]); // Option deprecated. In a future, billed must be managed with a dedicated field to 0 or 1
236}
237print $form->selectarray('object_status', $liststatus, $object_status, 1, 0, 0, '', 1);
238print '</td></tr>';
239// Year
240print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
241if (!in_array($year, $arrayyears)) {
242	$arrayyears[$year] = $year;
243}
244if (!in_array($nowyear, $arrayyears)) {
245	$arrayyears[$nowyear] = $nowyear;
246}
247arsort($arrayyears);
248print $form->selectarray('year', $arrayyears, $year, 0);
249print '</td></tr>';
250print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" 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="noborder centpercent">';
257print '<tr class="liste_titre" height="24">';
258print '<td class="center">'.$langs->trans("Year").'</td>';
259print '<td class="right">'.$langs->trans("NbOfinterventions").'</td>';
260print '<td class="right">%</td>';
261print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
262print '<td class="right">%</td>';
263print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
264print '<td class="right">%</td>';
265print '</tr>';
266
267$oldyear = 0;
268foreach ($data as $val) {
269	$year = $val['year'];
270	while (!empty($year) && $oldyear > $year + 1) {
271		// 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.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
276
277		print '<td class="right">0</td>';
278		print '<td class="right"></td>';
279		print '<td class="right">0</td>';
280		print '<td class="right"></td>';
281		print '<td class="right">0</td>';
282		print '<td class="right"></td>';
283		print '</tr>';
284	}
285
286
287	print '<tr class="oddeven" height="24">';
288	print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
289	print '<td class="right">'.$val['nb'].'</td>';
290	print '<td class="right" style="'.(($val['nb_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['nb_diff']).'</td>';
291	print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
292	print '<td class="right" style="'.(($val['total_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['total_diff']).'</td>';
293	print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
294	print '<td class="right" style="'.(($val['avg_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['avg_diff']).'</td>';
295	print '</tr>';
296	$oldyear = $year;
297}
298
299print '</table>';
300print '</div>';
301
302
303print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
304
305
306// Show graphs
307print '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
308if ($mesg) {
309	print $mesg;
310} else {
311	print $px1->show();
312	/*print "<br>\n";
313	print $px2->show();
314	print "<br>\n";
315	print $px3->show();*/
316}
317print '</td></tr></table>';
318
319
320print '</div></div></div>';
321print '<div style="clear:both"></div>';
322
323print dol_get_fiche_end();
324
325
326llxFooter();
327
328$db->close();
329