1<?php
2/* Copyright (C) 2014-2015 Florian HENRY       <florian.henry@open-concept.pro>
3 * Copyright (C) 2015-2021 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
36$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
37$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
38
39$userid = GETPOST('userid', 'int');
40$socid = GETPOST('socid', 'int');
41// Security check
42if ($user->socid > 0) {
43	$action = '';
44	$socid = $user->socid;
45}
46$nowyear = strftime("%Y", dol_now());
47$year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
48$startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
49$endyear = $year;
50
51// Load translation files required by the page
52$langs->loadLangs(array('companies', 'projects'));
53
54
55/*
56 * View
57 */
58
59$form = new Form($db);
60
61$includeuserlist = array();
62
63
64llxHeader('', $langs->trans('Projects'));
65
66$title = $langs->trans("ProjectsStatistics");
67$dir = $conf->project->dir_output.'/temp';
68
69print load_fiche_titre($title, '', 'project');
70
71dol_mkdir($dir);
72
73
74$stats_project = new ProjectStats($db);
75if (!empty($userid) && $userid != -1) {
76	$stats_project->userid = $userid;
77}
78if (!empty($socid) && $socid != -1) {
79	$stats_project->socid = $socid;
80}
81if (!empty($year)) {
82	$stats_project->year = $year;
83}
84
85/*
86if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
87{
88	// Current stats of project amount per status
89	$data1 = $stats_project->getAllProjectByStatus();
90
91	if (!is_array($data1) && $data1 < 0) {
92		setEventMessages($stats_project->error, null, 'errors');
93	}
94	if (empty($data1))
95	{
96		$showpointvalue = 0;
97		$nocolor = 1;
98		$data1 = array(array(0=>$langs->trans("None"), 1=>1));
99	}
100
101	$filenamenb = $conf->project->dir_output."/stats/projectbystatus.png";
102	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectbystatus.png';
103	$px = new DolGraph();
104	$mesg = $px->isGraphKo();
105	if (empty($mesg)) {
106		$i = 0; $tot = count($data1); $legend = array();
107		while ($i <= $tot)
108		{
109			$legend[] = $data1[$i][0];
110			$i++;
111		}
112
113		$px->SetData($data1);
114		unset($data1);
115
116		if ($nocolor)
117			$px->SetDataColor(array(
118					array(
119							220,
120							220,
121							220
122					)
123			));
124
125		$px->SetLegend($legend);
126		$px->setShowLegend(0);
127		$px->setShowPointValue($showpointvalue);
128		$px->setShowPercent(1);
129		$px->SetMaxValue($px->GetCeilMaxValue());
130		$px->SetWidth($WIDTH);
131		$px->SetHeight($HEIGHT);
132		$px->SetShading(3);
133		$px->SetHorizTickIncrement(1);
134		$px->SetCssPrefix("cssboxes");
135		$px->SetType(array('pie'));
136		$px->SetTitle($langs->trans('OpportunitiesStatusForProjects'));
137		$result = $px->draw($filenamenb, $fileurlnb);
138		if ($result < 0) {
139			setEventMessages($px->error, null, 'errors');
140		}
141	} else {
142		setEventMessages(null, $mesg, 'errors');
143	}
144}*/
145
146
147// Build graphic number of object
148// $data = array(array('Lib',val1,val2,val3),...)
149$data = $stats_project->getNbByMonthWithPrevYear($endyear, $startyear);
150//var_dump($data);
151
152$filenamenb = $conf->project->dir_output."/stats/projectnbprevyear-".$year.".png";
153$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectnbprevyear-'.$year.'.png';
154
155$px1 = new DolGraph();
156$mesg = $px1->isGraphKo();
157if (!$mesg) {
158	$px1->SetData($data);
159	$i = $startyear; $legend = array();
160	while ($i <= $endyear) {
161		$legend[] = $i;
162		$i++;
163	}
164	$px1->SetLegend($legend);
165	$px1->SetMaxValue($px1->GetCeilMaxValue());
166	$px1->SetWidth($WIDTH);
167	$px1->SetHeight($HEIGHT);
168	$px1->SetYLabel($langs->trans("ProjectNbProject"));
169	$px1->SetShading(3);
170	$px1->SetHorizTickIncrement(1);
171	$px1->mode = 'depth';
172	$px1->SetTitle($langs->trans("ProjectNbProjectByMonth"));
173
174	$px1->draw($filenamenb, $fileurlnb);
175}
176
177
178if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
179	// Build graphic amount of object
180	$data = $stats_project->getAmountByMonthWithPrevYear($endyear, $startyear);
181	//var_dump($data);
182	// $data = array(array('Lib',val1,val2,val3),...)
183
184	$filenamenb = $conf->project->dir_output."/stats/projectamountprevyear-".$year.".png";
185	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectamountprevyear-'.$year.'.png';
186
187	$px2 = new DolGraph();
188	$mesg = $px2->isGraphKo();
189	if (!$mesg) {
190		$i = $startyear; $legend = array();
191		while ($i <= $endyear) {
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	// Build graphic with transformation rate
215	$data = $stats_project->getWeightedAmountByMonthWithPrevYear($endyear, $startyear, 0, 0);
216	//var_dump($data);
217	// $data = array(array('Lib',val1,val2,val3),...)
218
219	$filenamenb = $conf->project->dir_output."/stats/projecttransrateprevyear-".$year.".png";
220	$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projecttransrateprevyear-'.$year.'.png';
221
222	$px3 = new DolGraph();
223	$mesg = $px3->isGraphKo();
224	if (!$mesg) {
225		$px3->SetData($data);
226		$i = $startyear;
227		$legend = array();
228		while ($i <= $endyear) {
229			$legend[] = $i;
230			$i++;
231		}
232		$px3->SetLegend($legend);
233		$px3->SetMaxValue($px3->GetCeilMaxValue());
234		$px3->SetMinValue(min(0, $px3->GetFloorMinValue()));
235		$px3->SetWidth($WIDTH);
236		$px3->SetHeight($HEIGHT);
237		$px3->SetYLabel($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
238		$px3->SetShading(3);
239		$px3->SetHorizTickIncrement(1);
240		$px3->mode = 'depth';
241		$px3->SetTitle($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
242
243		$px3->draw($filenamenb, $fileurlnb);
244	}
245}
246
247
248// Show array
249$stats_project->year = 0;
250$data_all_year = $stats_project->getAllByYear();
251
252if (!empty($year)) {
253	$stats_project->year = $year;
254}
255$arrayyears = array();
256foreach ($data_all_year as $val) {
257	$arrayyears[$val['year']] = $val['year'];
258}
259if (!count($arrayyears)) {
260	$arrayyears[$nowyear] = $nowyear;
261}
262
263
264$h = 0;
265$head = array();
266$head[$h][0] = DOL_URL_ROOT.'/projet/stats/index.php';
267$head[$h][1] = $langs->trans("ByMonthYear");
268$head[$h][2] = 'byyear';
269$h++;
270
271complete_head_from_modules($conf, $langs, null, $head, $h, 'project_stats');
272
273print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1, '');
274
275
276print '<div class="fichecenter"><div class="fichethirdleft">';
277
278print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
279print '<input type="hidden" name="token" value="'.newToken().'">';
280
281print '<table class="noborder centpercent">';
282print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
283// Company
284print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
285print img_picto('', 'company', 'class="pictofixedwidth"');
286print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
287print '</td></tr>';
288// User
289/*print '<tr><td>'.$langs->trans("ProjectCommercial").'</td><td>';
290print $form->select_dolusers($userid, 'userid', 1, array(),0,$includeuserlist);
291print '</td></tr>';*/
292// Year
293print '<tr><td>'.$langs->trans("Year").'</td><td>';
294if (!in_array($year, $arrayyears)) {
295	$arrayyears[$year] = $year;
296}
297if (!in_array($nowyear, $arrayyears)) {
298	$arrayyears[$nowyear] = $nowyear;
299}
300arsort($arrayyears);
301print $form->selectarray('year', $arrayyears, $year, 0);
302print '</td></tr>';
303print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
304print '</table>';
305print '</form>';
306print '<br><br>';
307
308print '<div class="div-table-responsive-no-min">';
309print '<table class="noborder centpercent">';
310print '<tr class="liste_titre" height="24">';
311print '<td class="center">'.$langs->trans("Year").'</td>';
312print '<td class="right">'.$langs->trans("NbOfProjects").'</td>';
313if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
314	print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
315	print '<td class="right">'.$langs->trans("OpportunityAmountAverageShort").'</td>';
316	print '<td class="right">'.$langs->trans("OpportunityAmountWeigthedShort").'</td>';
317}
318print '</tr>';
319
320$oldyear = 0;
321foreach ($data_all_year as $val) {
322	$year = $val['year'];
323	while ($year && $oldyear > $year + 1) {	// If we have empty year
324		$oldyear--;
325
326		print '<tr class="oddeven" height="24">';
327		print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
328		print '<td class="right">0</td>';
329		if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
330			print '<td class="right amount nowraponall">0</td>';
331			print '<td class="right amount nowraponall">0</td>';
332			print '<td class="right amount nowraponall">0</td>';
333		}
334		print '</tr>';
335	}
336
337	print '<tr class="oddeven" height="24">';
338	print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
339	print '<td class="right">'.$val['nb'].'</td>';
340	if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
341		print '<td class="right amount nowraponall">'.($val['total'] ? price(price2num($val['total'], 'MT'), 1) : '0').'</td>';
342		print '<td class="right amount nowraponall">'.($val['avg'] ? price(price2num($val['avg'], 'MT'), 1) : '0').'</td>';
343		print '<td class="right amount nowraponall">'.(isset($val['weighted']) ? price(price2num($val['weighted'], 'MT'), 1) : '0').'</td>';
344	}
345	print '</tr>';
346	$oldyear = $year;
347}
348
349print '</table>';
350print '</div>';
351
352print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
353
354$stringtoshow = '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
355if ($mesg) {
356	print $mesg;
357} else {
358	$stringtoshow .= $px1->show();
359	$stringtoshow .= "<br>\n";
360	if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
361		//$stringtoshow .= $px->show();
362		//$stringtoshow .= "<br>\n";
363		$stringtoshow .= $px2->show();
364		$stringtoshow .= "<br>\n";
365		$stringtoshow .= $px3->show();
366	}
367}
368$stringtoshow .= '</td></tr></table>';
369
370print $stringtoshow;
371
372
373print '</div></div></div>';
374print '<div style="clear:both"></div>';
375
376// End of page
377llxFooter();
378$db->close();
379