1<?php
2/* Copyright (C) 2014-2015 Florian HENRY       <florian.henry@open-concept.pro>
3 * Copyright (C) 2015      Laurent Destailleur <ldestailleur@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/**
20 *       \file       htdocs/projet/stats/index.php
21 *       \ingroup    project
22 *       \brief      Page for project statistics
23 */
24
25require '../../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/projet/class/projectstats.class.php';
29
30// Security check
31if (!$user->rights->projet->lire)
32	accessforbidden();
33
34
35$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
36$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
37
38$userid = GETPOST('userid', 'int');
39$socid = GETPOST('socid', 'int');
40// Security check
41if ($user->socid > 0)
42{
43	$action = '';
44	$socid = $user->socid;
45}
46$nowyear = strftime("%Y", dol_now());
47$year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
48//$startyear=$year-2;
49$startyear = $year - 1;
50$endyear = $year;
51
52// Load translation files required by the page
53$langs->loadLangs(array('companies', 'projects'));
54
55
56/*
57 * View
58 */
59
60$form = new Form($db);
61
62$includeuserlist = array();
63
64
65llxHeader('', $langs->trans('Projects'));
66
67$title = $langs->trans("ProjectsStatistics");
68$dir = $conf->projet->dir_output.'/temp';
69
70print load_fiche_titre($title, '', 'project');
71
72dol_mkdir($dir);
73
74
75$stats_project = new ProjectStats($db);
76if (!empty($userid) && $userid != -1) $stats_project->userid = $userid;
77if (!empty($socid) && $socid != -1) $stats_project->socid = $socid;
78if (!empty($year)) $stats_project->year = $year;
79
80/*
81if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
82{
83	// Current stats of project amount per status
84	$data1 = $stats_project->getAllProjectByStatus();
85
86	if (!is_array($data1) && $data1 < 0) {
87		setEventMessages($stats_project->error, null, 'errors');
88	}
89	if (empty($data1))
90	{
91		$showpointvalue = 0;
92		$nocolor = 1;
93		$data1 = array(array(0=>$langs->trans("None"), 1=>1));
94	}
95
96	$filenamenb = $conf->project->dir_output."/stats/projectbystatus.png";
97	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectbystatus.png';
98	$px = new DolGraph();
99	$mesg = $px->isGraphKo();
100	if (empty($mesg)) {
101		$i = 0; $tot = count($data1); $legend = array();
102		while ($i <= $tot)
103		{
104			$legend[] = $data1[$i][0];
105			$i++;
106		}
107
108		$px->SetData($data1);
109		unset($data1);
110
111		if ($nocolor)
112			$px->SetDataColor(array(
113					array(
114							220,
115							220,
116							220
117					)
118			));
119
120		$px->SetLegend($legend);
121		$px->setShowLegend(0);
122		$px->setShowPointValue($showpointvalue);
123		$px->setShowPercent(1);
124		$px->SetMaxValue($px->GetCeilMaxValue());
125		$px->SetWidth($WIDTH);
126		$px->SetHeight($HEIGHT);
127		$px->SetShading(3);
128		$px->SetHorizTickIncrement(1);
129		$px->SetCssPrefix("cssboxes");
130		$px->SetType(array('pie'));
131		$px->SetTitle($langs->trans('OpportunitiesStatusForProjects'));
132		$result = $px->draw($filenamenb, $fileurlnb);
133		if ($result < 0) {
134			setEventMessages($px->error, null, 'errors');
135		}
136	} else {
137		setEventMessages(null, $mesg, 'errors');
138	}
139}*/
140
141
142// Build graphic number of object
143// $data = array(array('Lib',val1,val2,val3),...)
144$data = $stats_project->getNbByMonthWithPrevYear($endyear, $startyear);
145//var_dump($data);
146
147$filenamenb = $conf->project->dir_output."/stats/projectnbprevyear-".$year.".png";
148$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectnbprevyear-'.$year.'.png';
149
150$px1 = new DolGraph();
151$mesg = $px1->isGraphKo();
152if (!$mesg)
153{
154	$px1->SetData($data);
155	$i = $startyear; $legend = array();
156	while ($i <= $endyear)
157	{
158		$legend[] = $i;
159		$i++;
160	}
161	$px1->SetLegend($legend);
162	$px1->SetMaxValue($px1->GetCeilMaxValue());
163	$px1->SetWidth($WIDTH);
164	$px1->SetHeight($HEIGHT);
165	$px1->SetYLabel($langs->trans("ProjectNbProject"));
166	$px1->SetShading(3);
167	$px1->SetHorizTickIncrement(1);
168	$px1->mode = 'depth';
169	$px1->SetTitle($langs->trans("ProjectNbProjectByMonth"));
170
171	$px1->draw($filenamenb, $fileurlnb);
172}
173
174
175if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
176{
177	// Build graphic amount of object
178	$data = $stats_project->getAmountByMonthWithPrevYear($endyear, $startyear);
179	//var_dump($data);
180	// $data = array(array('Lib',val1,val2,val3),...)
181
182	$filenamenb = $conf->project->dir_output."/stats/projectamountprevyear-".$year.".png";
183	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectamountprevyear-'.$year.'.png';
184
185	$px2 = new DolGraph();
186	$mesg = $px2->isGraphKo();
187	if (!$mesg)
188	{
189		$i = $startyear; $legend = array();
190		while ($i <= $endyear)
191		{
192			$legend[] = $i;
193			$i++;
194		}
195
196		$px2->SetData($data);
197		$px2->SetLegend($legend);
198		$px2->SetMaxValue($px2->GetCeilMaxValue());
199		$px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
200		$px2->SetWidth($WIDTH);
201		$px2->SetHeight($HEIGHT);
202		$px2->SetYLabel($langs->trans("ProjectOppAmountOfProjectsByMonth"));
203		$px2->SetShading(3);
204		$px2->SetHorizTickIncrement(1);
205		$px2->SetType(array('bars', 'bars'));
206		$px2->mode = 'depth';
207		$px2->SetTitle($langs->trans("ProjectOppAmountOfProjectsByMonth"));
208
209		$px2->draw($filenamenb, $fileurlnb);
210	}
211}
212
213if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
214{
215	// Build graphic with transformation rate
216	$data = $stats_project->getWeightedAmountByMonthWithPrevYear($endyear, $startyear, 0, 0);
217	//var_dump($data);
218	// $data = array(array('Lib',val1,val2,val3),...)
219
220	$filenamenb = $conf->project->dir_output."/stats/projecttransrateprevyear-".$year.".png";
221	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projecttransrateprevyear-'.$year.'.png';
222
223	$px3 = new DolGraph();
224	$mesg = $px3->isGraphKo();
225	if (!$mesg)
226	{
227		$px3->SetData($data);
228		$i = $startyear; $legend = array();
229		while ($i <= $endyear)
230		{
231			$legend[] = $i;
232			$i++;
233		}
234		$px3->SetLegend($legend);
235		$px3->SetMaxValue($px3->GetCeilMaxValue());
236		$px3->SetMinValue(min(0, $px3->GetFloorMinValue()));
237		$px3->SetWidth($WIDTH);
238		$px3->SetHeight($HEIGHT);
239		$px3->SetYLabel($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
240		$px3->SetShading(3);
241		$px3->SetHorizTickIncrement(1);
242		$px3->mode = 'depth';
243		$px3->SetTitle($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
244
245		$px3->draw($filenamenb, $fileurlnb);
246	}
247}
248
249
250// Show array
251$stats_project->year = 0;
252$data_all_year = $stats_project->getAllByYear();
253
254if (!empty($year)) $stats_project->year = $year;
255$arrayyears = array();
256foreach ($data_all_year as $val) {
257	$arrayyears[$val['year']] = $val['year'];
258}
259if (!count($arrayyears)) $arrayyears[$nowyear] = $nowyear;
260
261
262$h = 0;
263$head = array();
264$head[$h][0] = DOL_URL_ROOT.'/projet/stats/index.php';
265$head[$h][1] = $langs->trans("ByMonthYear");
266$head[$h][2] = 'byyear';
267$h++;
268
269complete_head_from_modules($conf, $langs, null, $head, $h, $type);
270
271print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1, '');
272
273
274print '<div class="fichecenter"><div class="fichethirdleft">';
275
276print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
277print '<input type="hidden" name="token" value="'.newToken().'">';
278
279print '<table class="noborder centpercent">';
280print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
281// Company
282print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
283print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, '', 'style="width: 95%"');
284print '</td></tr>';
285// User
286/*print '<tr><td>'.$langs->trans("ProjectCommercial").'</td><td>';
287print $form->select_dolusers($userid, 'userid', 1, array(),0,$includeuserlist);
288print '</td></tr>';*/
289// Year
290print '<tr><td>'.$langs->trans("Year").'</td><td>';
291if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year;
292if (!in_array($nowyear, $arrayyears)) $arrayyears[$nowyear] = $nowyear;
293arsort($arrayyears);
294print $form->selectarray('year', $arrayyears, $year, 0);
295print '</td></tr>';
296print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
297print '</table>';
298print '</form>';
299print '<br><br>';
300
301print '<div class="div-table-responsive-no-min">';
302print '<table class="noborder centpercent">';
303print '<tr class="liste_titre" height="24">';
304print '<td class="center">'.$langs->trans("Year").'</td>';
305print '<td class="right">'.$langs->trans("NbOfProjects").'</td>';
306if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
307{
308	print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
309	print '<td class="right">'.$langs->trans("OpportunityAmountAverageShort").'</td>';
310	print '<td class="right">'.$langs->trans("OpportunityAmountWeigthedShort").'</td>';
311}
312print '</tr>';
313
314$oldyear = 0;
315foreach ($data_all_year as $val)
316{
317	$year = $val['year'];
318	while ($year && $oldyear > $year + 1)
319	{	// If we have empty year
320		$oldyear--;
321
322		print '<tr class="oddeven" height="24">';
323		print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
324		if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
325		{
326			print '<td class="right">0</td>';
327			print '<td class="right">0</td>';
328			print '<td class="right">0</td>';
329		}
330		print '<td class="right">0</td>';
331		print '</tr>';
332	}
333
334	print '<tr class="oddeven" height="24">';
335	print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
336	print '<td class="right">'.$val['nb'].'</td>';
337	if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
338	{
339		print '<td class="right">'.($val['total'] ?price(price2num($val['total'], 'MT'), 1) : '0').'</td>';
340		print '<td class="right">'.($val['avg'] ?price(price2num($val['avg'], 'MT'), 1) : '0').'</td>';
341		print '<td class="right">'.($val['weighted'] ?price(price2num($val['weighted'], 'MT'), 1) : '0').'</td>';
342	}
343	print '</tr>';
344	$oldyear = $year;
345}
346
347print '</table>';
348print '</div>';
349
350print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
351
352$stringtoshow .= '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
353if ($mesg) { print $mesg; } else {
354	$stringtoshow .= $px1->show();
355	$stringtoshow .= "<br>\n";
356	if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
357	{
358		//$stringtoshow .= $px->show();
359		//$stringtoshow .= "<br>\n";
360		$stringtoshow .= $px2->show();
361		$stringtoshow .= "<br>\n";
362		$stringtoshow .= $px3->show();
363	}
364}
365$stringtoshow .= '</td></tr></table>';
366
367print $stringtoshow;
368
369
370print '</div></div></div>';
371print '<div style="clear:both"></div>';
372
373// End of page
374llxFooter();
375$db->close();
376