dolibarr  16.0.5
index.php
Go to the documentation of this file.
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 require '../../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacementstats.class.php';
29 
30 // Load translation files required by the page
31 $langs->loadLangs(array('trips', 'companies'));
32 
34 $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
35 
36 $userid = GETPOST('userid', 'int'); if ($userid < 0) {
37  $userid = 0;
38 }
39 $socid = GETPOST('socid', 'int'); if ($socid < 0) {
40  $socid = 0;
41 }
42 $id = GETPOST('id', 'int');
43 
44 // Security check
45 if ($user->socid > 0) {
46  $action = '';
47  $socid = $user->socid;
48 }
49 if ($user->socid) {
50  $socid = $user->socid;
51 }
52 $result = restrictedArea($user, 'deplacement', $id, '');
53 
54 // Other security check
55 $childids = $user->getAllChildIds();
56 $childids[] = $user->id;
57 if ($userid > 0) {
58  if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous) && !in_array($userid, $childids)) {
60  exit;
61  }
62 }
63 
64 $nowyear = strftime("%Y", dol_now());
65 $year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
66 $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
67 $endyear = $year;
68 
69 $mode = GETPOST("mode") ?GETPOST("mode") : 'customer';
70 
71 
72 /*
73  * View
74  */
75 
76 $form = new Form($db);
77 
78 
79 llxHeader();
80 
81 $title = $langs->trans("TripsAndExpensesStatistics");
82 $dir = $conf->deplacement->dir_temp;
83 
84 print load_fiche_titre($title, $mesg);
85 
86 dol_mkdir($dir);
87 
88 $useridtofilter = $userid; // Filter from parameters
89 if (empty($useridtofilter)) {
90  $useridtofilter = $childids;
91  if (!empty($user->rights->deplacement->readall) || !empty($user->rights->deplacement->lire_tous)) {
92  $useridtofilter = 0;
93  }
94 }
95 
96 $stats = new DeplacementStats($db, $socid, $useridtofilter);
97 
98 
99 // Build graphic number of object
100 // $data = array(array('Lib',val1,val2,val3),...)
101 //print "$endyear, $startyear";
102 $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
103 //var_dump($data);
104 
105 $filenamenb = $dir."/tripsexpensesnbinyear-".$year.".png";
106 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesnbinyear-'.$year.'.png';
107 
108 $px1 = new DolGraph();
109 $mesg = $px1->isGraphKo();
110 if (!$mesg) {
111  $px1->SetData($data);
112  $i = $startyear;
113  $legend = array();
114  while ($i <= $endyear) {
115  $legend[] = $i;
116  $i++;
117  }
118  $px1->SetLegend($legend);
119  $px1->SetMaxValue($px1->GetCeilMaxValue());
120  $px1->SetWidth($WIDTH);
121  $px1->SetHeight($HEIGHT);
122  $px1->SetYLabel($langs->trans("Number"));
123  $px1->SetShading(3);
124  $px1->SetHorizTickIncrement(1);
125  $px1->mode = 'depth';
126  $px1->SetTitle($langs->trans("NumberByMonth"));
127 
128  $px1->draw($filenamenb, $fileurlnb);
129 }
130 
131 // Build graphic amount of object
132 $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
133 //var_dump($data);
134 // $data = array(array('Lib',val1,val2,val3),...)
135 
136 $filenameamount = $dir."/tripsexpensesamountinyear-".$year.".png";
137 $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesamountinyear-'.$year.'.png';
138 
139 $px2 = new DolGraph();
140 $mesg = $px2->isGraphKo();
141 if (!$mesg) {
142  $px2->SetData($data);
143  $i = $startyear;
144  $legend = array();
145  while ($i <= $endyear) {
146  $legend[] = $i;
147  $i++;
148  }
149  $px2->SetLegend($legend);
150  $px2->SetMaxValue($px2->GetCeilMaxValue());
151  $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
152  $px2->SetWidth($WIDTH);
153  $px2->SetHeight($HEIGHT);
154  $px2->SetYLabel($langs->trans("Amount"));
155  $px2->SetShading(3);
156  $px2->SetHorizTickIncrement(1);
157  $px2->mode = 'depth';
158  $px2->SetTitle($langs->trans("AmountTotal"));
159 
160  $px2->draw($filenameamount, $fileurlamount);
161 }
162 
163 
164 $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
165 
166 if (empty($user->rights->societe->client->voir) || $user->socid) {
167  $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
168  if ($mode == 'customer') {
169  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
170  }
171  if ($mode == 'supplier') {
172  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
173  }
174 } else {
175  $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
176  if ($mode == 'customer') {
177  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
178  }
179  if ($mode == 'supplier') {
180  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
181  }
182 }
183 
184 $px3 = new DolGraph();
185 $mesg = $px3->isGraphKo();
186 if (!$mesg) {
187  $px3->SetData($data);
188  $i = $startyear;
189  $legend = array();
190  while ($i <= $endyear) {
191  $legend[] = $i;
192  $i++;
193  }
194  $px3->SetLegend($legend);
195  $px3->SetYLabel($langs->trans("AmountAverage"));
196  $px3->SetMaxValue($px3->GetCeilMaxValue());
197  $px3->SetMinValue($px3->GetFloorMinValue());
198  $px3->SetWidth($WIDTH);
199  $px3->SetHeight($HEIGHT);
200  $px3->SetShading(3);
201  $px3->SetHorizTickIncrement(1);
202  $px3->mode = 'depth';
203  $px3->SetTitle($langs->trans("AmountAverage"));
204 
205  $px3->draw($filename_avg, $fileurl_avg);
206 }
207 
208 
209 // Show array
210 $data = $stats->getAllByYear();
211 $arrayyears = array();
212 foreach ($data as $val) {
213  $arrayyears[$val['year']] = $val['year'];
214 }
215 if (!count($arrayyears)) {
216  $arrayyears[$nowyear] = $nowyear;
217 }
218 
219 
220 $h = 0;
221 $head = array();
222 $head[$h][0] = DOL_URL_ROOT.'/compta/deplacement/stats/index.php';
223 $head[$h][1] = $langs->trans("ByMonthYear");
224 $head[$h][2] = 'byyear';
225 $h++;
226 
227 complete_head_from_modules($conf, $langs, null, $head, $h, 'trip_stats');
228 
229 print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
230 
231 
232 print '<div class="fichecenter"><div class="fichethirdleft">';
233 
234 
235 // Show filter box
236 print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
237 print '<input type="hidden" name="token" value="'.newToken().'">';
238 print '<input type="hidden" name="mode" value="'.$mode.'">';
239 
240 print '<table class="border centpercent">';
241 print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
242 // Company
243 print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
244 $filter = '';
245 print img_picto('', 'company', 'class="pictofixedwidth"');
246 print $form->select_company($socid, 'socid', $filter, 1, 1, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
247 print '</td></tr>';
248 // User
249 print '<tr><td>'.$langs->trans("User").'</td><td>';
250 $include = '';
251 if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous)) {
252  $include = 'hierarchy';
253 }
254 print img_picto('', 'user', 'class="pictofixedwidth"');
255 print $form->select_dolusers($userid, 'userid', 1, '', 0, $include, '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
256 print '</td></tr>';
257 // Year
258 print '<tr><td>'.$langs->trans("Year").'</td><td>';
259 if (!in_array($year, $arrayyears)) {
260  $arrayyears[$year] = $year;
261 }
262 arsort($arrayyears);
263 print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
264 print '</td></tr>';
265 print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
266 print '</table>';
267 print '</form>';
268 print '<br><br>';
269 
270 print '<div class="div-table-responsive-no-min">';
271 print '<table class="border centpercent">';
272 print '<tr>';
273 print '<td class="center">'.$langs->trans("Year").'</td>';
274 print '<td class="center">'.$langs->trans("Number").'</td>';
275 print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
276 print '<td class="center">'.$langs->trans("AmountAverage").'</td>';
277 print '</tr>';
278 
279 $oldyear = 0;
280 foreach ($data as $val) {
281  $year = $val['year'];
282  while ($year && $oldyear > $year + 1) { // If we have empty year
283  $oldyear--;
284  print '<tr>';
285  print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.'">'.$oldyear.'</a></td>';
286  print '<td class="right">0</td>';
287  print '<td class="right">0</td>';
288  print '<td class="right">0</td>';
289  print '</tr>';
290  }
291 
292  // Total
293  print '<tr>';
294  print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.'">'.$year.'</a></td>';
295  print '<td class="right">'.$val['nb'].'</td>';
296  print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
297  print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
298  print '</tr>';
299  $oldyear = $year;
300 }
301 
302 print '</table>';
303 print '</div>';
304 
305 print '</div><div class="fichetwothirdright">';
306 
307 
308 // Show graphs
309 print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
310 if ($mesg) {
311  print $mesg;
312 } else {
313  print $px1->show();
314  print "<br>\n";
315  print $px2->show();
316  print "<br>\n";
317  print $px3->show();
318 }
319 print '</td></tr></table>';
320 
321 
322 print '</div></div>';
323 print '<div style="clear:both"></div>';
324 
325 
326 print dol_get_fiche_end();
327 
328 // End of page
329 llxFooter();
330 $db->close();
DolGraph\getDefaultGraphSizeForStats
static getDefaultGraphSizeForStats($direction, $defaultsize='')
getDefaultGraphSizeForStats
Definition: dolgraph.class.php:1539
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5190
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
DolGraph
Class to build graphs.
Definition: dolgraph.class.php:40
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
complete_head_from_modules
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add')
Complete or removed entries into a head array (used to build tabs).
Definition: functions.lib.php:9024
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5647
DeplacementStats
Classe permettant la gestion des stats des deplacements et notes de frais.
Definition: deplacementstats.class.php:31
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3866
llxFooter
llxFooter()
Footer empty.
Definition: index.php:71
llxHeader
if(!defined('NOTOKENRENEWAL')) if(!defined('NOLOGIN')) if(!defined('NOCSRFCHECK')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(!defined('NOIPCHECK')) if(!defined('NOBROWSERNOTIF')) llxHeader()
Header empty.
Definition: index.php:63
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1808
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2004
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2831
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6589