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  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.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 
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereportstats.class.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array('trips', 'companies'));
33 
35 $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
36 
37 $mode = GETPOSTISSET("mode") ? GETPOST("mode", 'aZ09') : 'customer';
38 $object_status = GETPOST('object_status', 'intcomma');
39 
40 $userid = GETPOST('userid', 'int');
41 $socid = GETPOST('socid', 'int'); if ($socid < 0) {
42  $socid = 0;
43 }
44 $id = GETPOST('id', 'int');
45 
46 // Security check
47 if ($user->socid > 0) {
48  $action = '';
49  $socid = $user->socid;
50 }
51 if ($user->socid) {
52  $socid = $user->socid;
53 }
54 $result = restrictedArea($user, 'expensereport', $id, '');
55 
56 $nowyear = strftime("%Y", dol_now());
57 $year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
58 $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
59 $endyear = $year;
60 
61 
62 
63 /*
64  * View
65  */
66 
67 $form = new Form($db);
68 $tmpexpensereport = new ExpenseReport($db);
69 
70 $title = $langs->trans("TripsAndExpensesStatistics");
71 $dir = $conf->expensereport->dir_temp;
72 
73 llxHeader('', $title);
74 
75 print load_fiche_titre($title, '', 'trip');
76 
77 dol_mkdir($dir);
78 
79 $stats = new ExpenseReportStats($db, $socid, $userid);
80 if ($object_status != '' && $object_status >= -1) {
81  $stats->where .= ' AND e.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
82 }
83 
84 // Build graphic number of object
85 // $data = array(array('Lib',val1,val2,val3),...)
86 //print "$endyear, $startyear";
87 $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
88 //var_dump($data);
89 
90 $filenamenb = $dir."/tripsexpensesnbinyear-".$year.".png";
91 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesnbinyear-'.$year.'.png';
92 
93 $px1 = new DolGraph();
94 $mesg = $px1->isGraphKo();
95 if (!$mesg) {
96  $px1->SetData($data);
97  $i = $startyear; $legend = array();
98  while ($i <= $endyear) {
99  $legend[] = $i;
100  $i++;
101  }
102  $px1->SetLegend($legend);
103  $px1->SetMaxValue($px1->GetCeilMaxValue());
104  $px1->SetWidth($WIDTH);
105  $px1->SetHeight($HEIGHT);
106  $px1->SetYLabel($langs->trans("Number"));
107  $px1->SetShading(3);
108  $px1->SetHorizTickIncrement(1);
109  $px1->mode = 'depth';
110  $px1->SetTitle($langs->trans("NumberByMonth"));
111 
112  $px1->draw($filenamenb, $fileurlnb);
113 }
114 
115 // Build graphic amount of object
116 $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
117 //var_dump($data);
118 // $data = array(array('Lib',val1,val2,val3),...)
119 
120 $filenameamount = $dir."/tripsexpensesamountinyear-".$year.".png";
121 $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesamountinyear-'.$year.'.png';
122 
123 $px2 = new DolGraph();
124 $mesg = $px2->isGraphKo();
125 if (!$mesg) {
126  $px2->SetData($data);
127  $i = $startyear; $legend = array();
128  while ($i <= $endyear) {
129  $legend[] = $i;
130  $i++;
131  }
132  $px2->SetLegend($legend);
133  $px2->SetMaxValue($px2->GetCeilMaxValue());
134  $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
135  $px2->SetWidth($WIDTH);
136  $px2->SetHeight($HEIGHT);
137  $px2->SetYLabel($langs->trans("Amount"));
138  $px2->SetShading(3);
139  $px2->SetHorizTickIncrement(1);
140  $px2->mode = 'depth';
141  $px2->SetTitle($langs->trans("AmountTotal"));
142 
143  $px2->draw($filenameamount, $fileurlamount);
144 }
145 
146 
147 $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
148 
149 if (empty($user->rights->societe->client->voir) || $user->socid) {
150  $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
151  if ($mode == 'customer') {
152  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
153  }
154  if ($mode == 'supplier') {
155  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
156  }
157 } else {
158  $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
159  if ($mode == 'customer') {
160  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
161  }
162  if ($mode == 'supplier') {
163  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
164  }
165 }
166 
167 $px3 = new DolGraph();
168 $mesg = $px3->isGraphKo();
169 if (!$mesg) {
170  $px3->SetData($data);
171  $i = $startyear; $legend = array();
172  while ($i <= $endyear) {
173  $legend[] = $i;
174  $i++;
175  }
176  $px3->SetLegend($legend);
177  $px3->SetYLabel($langs->trans("AmountAverage"));
178  $px3->SetMaxValue($px3->GetCeilMaxValue());
179  $px3->SetMinValue($px3->GetFloorMinValue());
180  $px3->SetWidth($WIDTH);
181  $px3->SetHeight($HEIGHT);
182  $px3->SetShading(3);
183  $px3->SetHorizTickIncrement(1);
184  $px3->mode = 'depth';
185  $px3->SetTitle($langs->trans("AmountAverage"));
186 
187  $px3->draw($filename_avg, $fileurl_avg);
188 }
189 
190 
191 // Show array
192 $data = $stats->getAllByYear();
193 $arrayyears = array();
194 foreach ($data as $val) {
195  $arrayyears[$val['year']] = $val['year'];
196 }
197 if (!count($arrayyears)) {
198  $arrayyears[$nowyear] = $nowyear;
199 }
200 
201 
202 $h = 0;
203 $head = array();
204 $head[$h][0] = DOL_URL_ROOT.'/expensereport/stats/index.php';
205 $head[$h][1] = $langs->trans("ByMonthYear");
206 $head[$h][2] = 'byyear';
207 $h++;
208 
209 complete_head_from_modules($conf, $langs, null, $head, $h, 'trip_stats');
210 
211 print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
212 
213 
214 print '<div class="fichecenter"><div class="fichethirdleft">';
215 
216 
217 // Show filter box
218 print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
219 print '<input type="hidden" name="token" value="'.newToken().'">';
220 print '<input type="hidden" name="mode" value="'.$mode.'">';
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 /*
226 print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
227 print $form->select_company($socid,'socid','',1,1,0,array(),0,'widthcentpercentminusx maxwidth300','');
228 print '</td></tr>';
229 */
230 // User
231 print '<tr><td>'.$langs->trans("User").'</td><td>';
232 $include = '';
233 if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)) {
234  $include = 'hierarchy';
235 }
236 print img_picto('', 'user', 'class="pictofixedwidth"');
237 print $form->select_dolusers($userid, 'userid', 1, '', 0, $include, '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
238 print '</td></tr>';
239 // Status
240 print '<tr><td class="left">'.$langs->trans("Status").'</td><td class="left">';
241 $liststatus = $tmpexpensereport->statuts;
242 print $form->selectarray('object_status', $liststatus, GETPOST('object_status', 'intcomma'), -4, 0, 0, '', 1);
243 print '</td></tr>';
244 // Year
245 print '<tr><td>';
246 print $form->textwithpicto($langs->trans("Year"), $langs->trans("DateValidation"));
247 print '</td><td>';
248 if (!in_array($year, $arrayyears)) {
249  $arrayyears[$year] = $year;
250 }
251 arsort($arrayyears);
252 print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
253 print '</td></tr>';
254 print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
255 print '</table>';
256 print '</form>';
257 print '<br><br>';
258 
259 print '<div class="div-table-responsive-no-min">';
260 print '<table class="noborder centpercent">';
261 print '<tr class="liste_titre" height="24">';
262 print '<td class="center">'.$langs->trans("Year").'</td>';
263 print '<td class="right">'.$langs->trans("Number").'</td>';
264 print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
265 print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
266 print '</tr>';
267 
268 $oldyear = 0;
269 foreach ($data as $val) {
270  $year = $val['year'];
271  while ($year && $oldyear > $year + 1) { // 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.'">'.$oldyear.'</a></td>';
276  print '<td class="right">0</td>';
277  print '<td class="right amount nowraponall">0</td>';
278  print '<td class="right amount nowraponall">0</td>';
279  print '</tr>';
280  }
281 
282 
283  print '<tr class="oddeven" height="24">';
284  print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.'">'.$year.'</a></td>';
285  print '<td class="right">'.$val['nb'].'</td>';
286  print '<td class="right amount nowraponall">'.price(price2num($val['total'], 'MT'), 1).'</td>';
287  print '<td class="right amount nowraponall">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
288  print '</tr>';
289  $oldyear = $year;
290 }
291 
292 print '</table>';
293 print '</div>';
294 
295 print '</div><div class="fichetwothirdright">';
296 
297 
298 // Show graphs
299 print '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
300 if ($mesg) {
301  print $mesg;
302 } else {
303  print $px1->show();
304  print "<br>\n";
305  print $px2->show();
306  print "<br>\n";
307  print $px3->show();
308 }
309 print '</td></tr></table>';
310 
311 
312 print '</div></div>';
313 print '<div style="clear:both"></div>';
314 
315 
316 print dol_get_fiche_end();
317 
318 // End of page
319 llxFooter();
320 $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:5204
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
ExpenseReportStats
Classe permettant la gestion des stats des expensereports et notes de frais.
Definition: expensereportstats.class.php:31
$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:9038
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
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:3880
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:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
ExpenseReport
Class to manage Trips and Expenses.
Definition: expensereport.class.php:36
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6603