1<?php
2/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2016 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin        <regis.houssin@capnetworks.com>
5 * Copyright (C) 2018	   Quentin Vial-Gouteyron    <quentin.vial-gouteyron@atm-consulting.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/reception/stats/index.php
23 *     \ingroup    reception
24 *     \brief      Page with reception statistics
25 */
26
27require '../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
29require_once DOL_DOCUMENT_ROOT.'/reception/class/receptionstats.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
31
32$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
33$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
34
35$userid = GETPOST('userid', 'int');
36$socid = GETPOST('socid', 'int');
37// Security check
38if ($user->socid > 0)
39{
40	$action = '';
41	$socid = $user->socid;
42}
43
44$nowyear = strftime("%Y", dol_now());
45$year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
46//$startyear=$year-2;
47$startyear = $year - 1;
48$endyear = $year;
49
50$langs->load("reception");
51$langs->load("other");
52$langs->load("companies");
53
54
55
56/*
57 * View
58 */
59
60$form = new Form($db);
61
62llxHeader();
63
64print load_fiche_titre($langs->trans("StatisticsOfReceptions"), '', 'dollyrevert');
65
66
67dol_mkdir($dir);
68
69$stats = new ReceptionStats($db, $socid, '', ($userid > 0 ? $userid : 0));
70
71// Build graphic number of object
72$data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
73//var_dump($data);exit;
74// $data = array(array('Lib',val1,val2,val3),...)
75
76
77if (!$user->rights->societe->client->voir || $user->socid)
78{
79	$filenamenb = $dir.'/receptionsnbinyear-'.$user->id.'-'.$year.'.png';
80} else {
81	$filenamenb = $dir.'/receptionsnbinyear-'.$year.'.png';
82}
83
84$px1 = new DolGraph();
85$mesg = $px1->isGraphKo();
86if (!$mesg)
87{
88	$px1->SetData($data);
89	$i = $startyear; $legend = array();
90	while ($i <= $endyear)
91	{
92		$legend[] = $i;
93		$i++;
94	}
95	$px1->SetLegend($legend);
96	$px1->SetMaxValue($px1->GetCeilMaxValue());
97	$px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
98	$px1->SetWidth($WIDTH);
99	$px1->SetHeight($HEIGHT);
100	$px1->SetYLabel($langs->trans("NbOfReceptions"));
101	$px1->SetShading(3);
102	$px1->SetHorizTickIncrement(1);
103	$px1->mode = 'depth';
104	$px1->SetTitle($langs->trans("NumberOfReceptionsByMonth"));
105
106	$px1->draw($filenamenb, $fileurlnb);
107}
108
109// Build graphic amount of object
110/*
111$data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
112//var_dump($data);
113// $data = array(array('Lib',val1,val2,val3),...)
114
115if (!$user->rights->societe->client->voir || $user->socid)
116{
117    $filenameamount = $dir.'/receptionsamountinyear-'.$user->id.'-'.$year.'.png';
118}
119else
120{
121    $filenameamount = $dir.'/receptionsamountinyear-'.$year.'.png';
122}
123
124$px2 = new DolGraph();
125$mesg = $px2->isGraphKo();
126if (! $mesg)
127{
128    $px2->SetData($data);
129    $i=$startyear;$legend=array();
130    while ($i <= $endyear)
131    {
132        $legend[]=$i;
133        $i++;
134    }
135    $px2->SetLegend($legend);
136    $px2->SetMaxValue($px2->GetCeilMaxValue());
137    $px2->SetMinValue(min(0,$px2->GetFloorMinValue()));
138    $px2->SetWidth($WIDTH);
139    $px2->SetHeight($HEIGHT);
140    $px2->SetYLabel($langs->trans("AmountOfReceptions"));
141    $px2->SetShading(3);
142    $px2->SetHorizTickIncrement(1);
143    $px2->mode='depth';
144    $px2->SetTitle($langs->trans("AmountOfReceptionsByMonthHT"));
145
146    $px2->draw($filenameamount,$fileurlamount);
147}
148*/
149
150/*
151$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
152
153if (!$user->rights->societe->client->voir || $user->socid)
154{
155    $filename_avg = $dir.'/receptionsaverage-'.$user->id.'-'.$year.'.png';
156}
157else
158{
159    $filename_avg = $dir.'/receptionsaverage-'.$year.'.png';
160}
161
162$px3 = new DolGraph();
163$mesg = $px3->isGraphKo();
164if (! $mesg)
165{
166    $px3->SetData($data);
167    $i=$startyear;$legend=array();
168    while ($i <= $endyear)
169    {
170        $legend[]=$i;
171        $i++;
172    }
173    $px3->SetLegend($legend);
174    $px3->SetYLabel($langs->trans("AmountAverage"));
175    $px3->SetMaxValue($px3->GetCeilMaxValue());
176    $px3->SetMinValue($px3->GetFloorMinValue());
177    $px3->SetWidth($WIDTH);
178    $px3->SetHeight($HEIGHT);
179    $px3->SetShading(3);
180    $px3->SetHorizTickIncrement(1);
181    $px3->mode='depth';
182    $px3->SetTitle($langs->trans("AmountAverage"));
183
184    $px3->draw($filename_avg,$fileurl_avg);
185}
186*/
187
188
189// Show array
190$data = $stats->getAllByYear();
191$arrayyears = array();
192foreach ($data as $val) {
193	if (!empty($val['year'])) {
194		$arrayyears[$val['year']] = $val['year'];
195	}
196}
197if (!count($arrayyears)) $arrayyears[$nowyear] = $nowyear;
198
199$h = 0;
200$head = array();
201$head[$h][0] = DOL_URL_ROOT.'/reception/stats/index.php';
202$head[$h][1] = $langs->trans("ByMonthYear");
203$head[$h][2] = 'byyear';
204$h++;
205
206$type = 'reception_stats';
207
208complete_head_from_modules($conf, $langs, null, $head, $h, $type);
209
210print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
211
212
213print '<div class="fichecenter"><div class="fichethirdleft">';
214
215
216//if (empty($socid))
217//{
218	// Show filter box
219	print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
220	print '<input type="hidden" name="token" value="'.newToken().'">';
221
222	print '<table class="noborder centpercent">';
223	print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
224	// Company
225	print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
226	print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, '', 'style="width: 95%"');
227	print '</td></tr>';
228	// User
229	print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
230	print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
231	print '</td></tr>';
232	// Year
233	print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
234	if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year;
235	if (!in_array($nowyear, $arrayyears)) $arrayyears[$nowyear] = $nowyear;
236	arsort($arrayyears);
237	print $form->selectarray('year', $arrayyears, $year, 0);
238	print '</td></tr>';
239	print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
240	print '</table>';
241	print '</form>';
242	print '<br><br>';
243//}
244
245print '<table class="noborder centpercent">';
246print '<tr class="liste_titre" height="24">';
247print '<td class="center">'.$langs->trans("Year").'</td>';
248print '<td class="right">'.$langs->trans("NbOfReceptions").'</td>';
249/*print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
250print '<td class="center">'.$langs->trans("AmountAverage").'</td>';*/
251print '</tr>';
252
253$oldyear = 0;
254foreach ($data as $val)
255{
256	$year = $val['year'];
257	while (!empty($year) && $oldyear > $year + 1)
258	{ // If we have empty year
259		$oldyear--;
260
261
262		print '<tr class="oddeven" height="24">';
263		print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'">'.$oldyear.'</a></td>';
264
265		print '<td class="right">0</td>';
266		/*print '<td class="right">0</td>';
267		print '<td class="right">0</td>';*/
268		print '</tr>';
269	}
270
271	print '<tr class="oddeven" height="24">';
272	print '<td class="center">';
273	if ($year) print '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'">'.$year.'</a>';
274	else print $langs->trans("ValidationDateNotDefinedEvenIfReceptionValidated");
275	print '</td>';
276	print '<td class="right">'.$val['nb'].'</td>';
277	/*print '<td class="right">'.price(price2num($val['total'],'MT'),1).'</td>';
278	print '<td class="right">'.price(price2num($val['avg'],'MT'),1).'</td>';*/
279	print '</tr>';
280	$oldyear = $year;
281}
282
283print '</table>';
284
285
286print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
287
288
289// Show graphs
290print '<table class="border centpercent"><tr valign="top"><td class="center">';
291if ($mesg) { print $mesg; } else {
292	print $px1->show();
293	print "<br>\n";
294	/*print $px2->show();
295    print "<br>\n";
296    print $px3->show();*/
297}
298print '</td></tr></table>';
299
300
301print '</div></div></div>';
302print '<div style="clear:both"></div>';
303
304print dol_get_fiche_end();
305
306
307
308// TODO USe code similar to commande/stats/index.php instead of this one.
309/*
310print '<table class="border centpercent">';
311print '<tr><td class="center">'.$langs->trans("Year").'</td>';
312print '<td width="40%" class="center">'.$langs->trans("NbOfReceptions").'</td></tr>';
313
314$sql = "SELECT count(*) as nb, date_format(date_reception,'%Y') as dm";
315$sql.= " FROM ".MAIN_DB_PREFIX."reception";
316$sql.= " WHERE fk_statut > 0";
317$sql.= " AND entity = ".$conf->entity;
318$sql.= " GROUP BY dm DESC";
319
320$resql=$db->query($sql);
321if ($resql)
322{
323    $num = $db->num_rows($resql);
324    $i = 0;
325    while ($i < $num)
326    {
327        $row = $db->fetch_row($resql);
328        $nbproduct = $row[0];
329        $year = $row[1];
330        print "<tr>";
331        print '<td class="center"><a href="month.php?year='.$year.'">'.$year.'</a></td><td class="center">'.$nbproduct.'</td></tr>';
332        $i++;
333    }
334}
335$db->free($resql);
336
337print '</table>';
338*/
339
340print '<br>';
341print '<i class="opacitymedium">'.$langs->trans("StatsOnReceptionsOnlyValidated").'</i>';
342
343llxFooter();
344
345$db->close();
346