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) 2013 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
7  * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 require '../../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
35 if (isModEnabled('categorie')) {
36  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
37 }
38 
40 $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
41 
42 // Load translation files required by the page
43 $langs->loadLangs(array('bills', 'companies', 'other'));
44 
45 $mode = GETPOST("mode") ? GETPOST("mode") : 'customer';
46 if ($mode == 'customer' && !$user->rights->facture->lire) {
48 }
49 if ($mode == 'supplier' && empty($user->rights->fournisseur->facture->lire)) {
51 }
52 
53 $object_status = GETPOST('object_status', 'intcomma');
54 $typent_id = GETPOST('typent_id', 'int');
55 $categ_id = GETPOST('categ_id', 'categ_id');
56 
57 $userid = GETPOST('userid', 'int');
58 $socid = GETPOST('socid', 'int');
59 $custcats = GETPOST('custcats', 'array');
60 // Security check
61 if ($user->socid > 0) {
62  $action = '';
63  $socid = $user->socid;
64 }
65 
66 $nowyear = strftime("%Y", dol_now());
67 $year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
68 $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
69 $endyear = $year;
70 
71 
72 /*
73  * View
74  */
75 if (isModEnabled('categorie')) {
76  $langs->load('categories');
77 }
78 $form = new Form($db);
79 $formcompany = new FormCompany($db);
80 $formother = new FormOther($db);
81 
82 llxHeader();
83 
84 $picto = 'bill';
85 $title = $langs->trans("BillsStatistics");
86 $dir = $conf->facture->dir_temp;
87 
88 if ($mode == 'supplier') {
89  $picto = 'supplier_invoice';
90  $title = $langs->trans("BillsStatisticsSuppliers");
91  $dir = $conf->fournisseur->facture->dir_temp;
92 }
93 
94 
95 print load_fiche_titre($title, '', $picto);
96 
97 dol_mkdir($dir);
98 
99 $stats = new FactureStats($db, $socid, $mode, ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
100 if ($mode == 'customer') {
101  if ($object_status != '' && $object_status >= 0) {
102  $stats->where .= ' AND f.fk_statut IN ('.$db->sanitize($object_status).')';
103  }
104  if (is_array($custcats) && !empty($custcats)) {
105  $stats->from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cat ON (f.fk_soc = cat.fk_soc)';
106  $stats->where .= ' AND cat.fk_categorie IN ('.$db->sanitize(implode(',', $custcats)).')';
107  }
108 }
109 if ($mode == 'supplier') {
110  if ($object_status != '' && $object_status >= 0) {
111  $stats->where .= ' AND f.fk_statut IN ('.$db->sanitize($object_status).')';
112  }
113  if (is_array($custcats) && !empty($custcats)) {
114  $stats->from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_fournisseur as cat ON (f.fk_soc = cat.fk_soc)';
115  $stats->where .= ' AND cat.fk_categorie IN ('.$db->sanitize(implode(',', $custcats)).')';
116  }
117 }
118 
119 // Build graphic number of object
120 // $data = array(array('Lib',val1,val2,val3),...)
121 $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
122 //var_dump($data);
123 
124 $filenamenb = $dir."/invoicesnbinyear-".$year.".png";
125 if ($mode == 'customer') {
126  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesnbinyear-'.$year.'.png';
127 }
128 if ($mode == 'supplier') {
129  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicesnbinyear-'.$year.'.png';
130 }
131 
132 $px1 = new DolGraph();
133 $mesg = $px1->isGraphKo();
134 if (!$mesg) {
135  $px1->SetData($data);
136  $i = $startyear;
137  $legend = array();
138  while ($i <= $endyear) {
139  $legend[] = $i;
140  $i++;
141  }
142  $px1->SetLegend($legend);
143  $px1->SetMaxValue($px1->GetCeilMaxValue());
144  $px1->SetWidth($WIDTH);
145  $px1->SetHeight($HEIGHT);
146  $px1->SetYLabel($langs->trans("NumberOfBills"));
147  $px1->SetShading(3);
148  $px1->SetHorizTickIncrement(1);
149  $px1->mode = 'depth';
150  $px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
151 
152  $px1->draw($filenamenb, $fileurlnb);
153 }
154 
155 // Build graphic amount of object
156 $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
157 //var_dump($data);
158 // $data = array(array('Lib',val1,val2,val3),...)
159 
160 $filenameamount = $dir."/invoicesamountinyear-".$year.".png";
161 if ($mode == 'customer') {
162  $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesamountinyear-'.$year.'.png';
163 }
164 if ($mode == 'supplier') {
165  $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicesamountinyear-'.$year.'.png';
166 }
167 
168 $px2 = new DolGraph();
169 $mesg = $px2->isGraphKo();
170 if (!$mesg) {
171  $px2->SetData($data);
172  $i = $startyear;
173  $legend = array();
174  while ($i <= $endyear) {
175  $legend[] = $i;
176  $i++;
177  }
178  $px2->SetLegend($legend);
179  $px2->SetMaxValue($px2->GetCeilMaxValue());
180  $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
181  $px2->SetWidth($WIDTH);
182  $px2->SetHeight($HEIGHT);
183  $px2->SetYLabel($langs->trans("AmountOfBills"));
184  $px2->SetShading(3);
185  $px2->SetHorizTickIncrement(1);
186  $px2->mode = 'depth';
187  $px2->SetTitle($langs->trans("AmountOfBillsByMonthHT"));
188 
189  $px2->draw($filenameamount, $fileurlamount);
190 }
191 
192 
193 $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
194 
195 if (empty($user->rights->societe->client->voir) || $user->socid) {
196  $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
197  if ($mode == 'customer') {
198  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
199  }
200  if ($mode == 'supplier') {
201  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
202  }
203 } else {
204  $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
205  if ($mode == 'customer') {
206  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
207  }
208  if ($mode == 'supplier') {
209  $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
210  }
211 }
212 
213 $px3 = new DolGraph();
214 $mesg = $px3->isGraphKo();
215 if (!$mesg) {
216  $px3->SetData($data);
217  $i = $startyear;
218  $legend = array();
219  while ($i <= $endyear) {
220  $legend[] = $i;
221  $i++;
222  }
223  $px3->SetLegend($legend);
224  $px3->SetYLabel($langs->trans("AmountAverage"));
225  $px3->SetMaxValue($px3->GetCeilMaxValue());
226  $px3->SetMinValue($px3->GetFloorMinValue());
227  $px3->SetWidth($WIDTH);
228  $px3->SetHeight($HEIGHT);
229  $px3->SetShading(3);
230  $px3->SetHorizTickIncrement(1);
231  $px3->mode = 'depth';
232  $px3->SetTitle($langs->trans("AmountAverage"));
233 
234  $px3->draw($filename_avg, $fileurl_avg);
235 }
236 
237 
238 // Show array
239 $data = $stats->getAllByYear();
240 $arrayyears = array();
241 foreach ($data as $val) {
242  $arrayyears[$val['year']] = $val['year'];
243 }
244 if (!count($arrayyears)) {
245  $arrayyears[$nowyear] = $nowyear;
246 }
247 
248 
249 $h = 0;
250 $head = array();
251 $head[$h][0] = DOL_URL_ROOT.'/compta/facture/stats/index.php?mode='.urlencode($mode);
252 $head[$h][1] = $langs->trans("ByMonthYear");
253 $head[$h][2] = 'byyear';
254 $h++;
255 
256 if ($mode == 'customer') {
257  $type = 'invoice_stats';
258 }
259 if ($mode == 'supplier') {
260  $type = 'supplier_invoice_stats';
261 }
262 
263 complete_head_from_modules($conf, $langs, null, $head, $h, $type);
264 
265 print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
266 
267 // We use select_thirdparty_list instead of select_company so we can use $filter and share same code for customer and supplier.
268 $filter = '';
269 if ($mode == 'customer') {
270  $filter = 's.client in (1,2,3)';
271 }
272 if ($mode == 'supplier') {
273  $filter = 's.fournisseur = 1';
274 }
275 
276 print '<div class="fichecenter"><div class="fichethirdleft">';
277 
278 
279 // Show filter box
280 print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
281 print '<input type="hidden" name="token" value="'.newToken().'">';
282 print '<input type="hidden" name="mode" value="'.$mode.'">';
283 
284 print '<table class="noborder centpercent">';
285 print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
286 // Company
287 print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
288 print img_picto('', 'company', 'class="pictofixedwidth"');
289 print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300');
290 print '</td></tr>';
291 
292 // ThirdParty Type
293 print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
294 $sortparam_typent = (empty($conf->global->SOCIETE_SORT_ON_TYPEENT) ? 'ASC' : $conf->global->SOCIETE_SORT_ON_TYPEENT); // NONE means we keep sort of original array, so we sort on position. ASC, means next function will sort on label.
295 print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0, '', 0, 0, 0, $sortparam_typent, '', 1);
296 if ($user->admin) {
297  print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
298 }
299 print '</td></tr>';
300 
301 // Category
302 if (isModEnabled('categorie')) {
303  if ($mode == 'customer') {
304  $cat_type = Categorie::TYPE_CUSTOMER;
305  $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
306  }
307  if ($mode == 'supplier') {
308  $cat_type = Categorie::TYPE_SUPPLIER;
309  $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
310  }
311  print '<tr><td>'.$cat_label.'</td><td>';
312  $cate_arbo = $form->select_all_categories($cat_type, null, 'parent', null, null, 1);
313  print img_picto('', 'category', 'class="pictofixedwidth"');
314  print $form->multiselectarray('custcats', $cate_arbo, GETPOST('custcats', 'array'), 0, 0, 'widthcentpercentminusx maxwidth300');
315  //print $formother->select_categories($cat_type, $categ_id, 'categ_id', true);
316  print '</td></tr>';
317 }
318 
319 // User
320 print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
321 print img_picto('', 'user', 'class="pictofixedwidth"');
322 print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
323 print '</td></tr>';
324 // Status
325 print '<tr><td>'.$langs->trans("Status").'</td><td>';
326 if ($mode == 'customer') {
327  $liststatus = array('0'=>$langs->trans("BillStatusDraft"), '1'=>$langs->trans("BillStatusNotPaid"), '2'=>$langs->trans("BillStatusPaid"), '1,2'=>$langs->trans("BillStatusNotPaid").' / '.$langs->trans("BillStatusPaid"), '3'=>$langs->trans("BillStatusCanceled"));
328  print $form->selectarray('object_status', $liststatus, $object_status, 1);
329 }
330 if ($mode == 'supplier') {
331  $liststatus = array('0'=>$langs->trans("BillStatusDraft"), '1'=>$langs->trans("BillStatusNotPaid"), '2'=>$langs->trans("BillStatusPaid"));
332  print $form->selectarray('object_status', $liststatus, $object_status, 1);
333 }
334 print '</td></tr>';
335 // Year
336 print '<tr><td>'.$langs->trans("Year").'</td><td>';
337 if (!in_array($year, $arrayyears)) {
338  $arrayyears[$year] = $year;
339 }
340 if (!in_array($nowyear, $arrayyears)) {
341  $arrayyears[$nowyear] = $nowyear;
342 }
343 arsort($arrayyears);
344 print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
345 print '</td></tr>';
346 print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
347 print '</table>';
348 print '</form>';
349 print '<br><br>';
350 
351 print '<div class="div-table-responsive-no-min">';
352 print '<table class="noborder centpercent">';
353 print '<tr class="liste_titre" height="24">';
354 print '<td class="center">'.$langs->trans("Year").'</td>';
355 print '<td class="right">'.$langs->trans("NumberOfBills").'</td>';
356 print '<td class="right">%</td>';
357 print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
358 print '<td class="right">%</td>';
359 print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
360 print '<td class="right">%</td>';
361 print '</tr>';
362 
363 $oldyear = 0;
364 foreach ($data as $val) {
365  $year = $val['year'];
366  while ($year && $oldyear > $year + 1) { // If we have empty year
367  $oldyear--;
368 
369  print '<tr class="oddeven" height="24">';
370  print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
371  print '<td class="right">0</td>';
372  print '<td class="right"></td>';
373  print '<td class="right amount">0</td>';
374  print '<td class="right"></td>';
375  print '<td class="right amount">0</td>';
376  print '<td class="right"></td>';
377  print '</tr>';
378  }
379 
380  print '<tr class="oddeven" height="24">';
381  print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
382  print '<td class="right">'.$val['nb'].'</td>';
383  print '<td class="right opacitylow" style="'.((empty($val['nb_diff']) || $val['nb_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(!empty($val['nb_diff']) && $val['nb_diff'] < 0 ? '' : '+').round(!empty($val['nb_diff']) ? $val['nb_diff'] : 0).'%</td>';
384  print '<td class="right"><span class="amount">'.price(price2num($val['total'], 'MT'), 1).'</span></td>';
385  print '<td class="right opacitylow" style="'.((empty($val['total_diff']) || $val['total_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.( !empty($val['total_diff']) && $val['total_diff'] < 0 ? '' : '+').round(!empty($val['total_diff']) ? $val['total_diff'] : 0).'%</td>';
386  print '<td class="right"><span class="amount">'.price(price2num($val['avg'], 'MT'), 1).'</span></td>';
387  print '<td class="right opacitylow" style="'.((empty($val['avg_diff']) || $val['avg_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(!empty($val['avg_diff']) && $val['avg_diff'] < 0 ? '' : '+').round(!empty($val['avg_diff']) ? $val['avg_diff'] : 0).'%</td>';
388  print '</tr>';
389  $oldyear = $year;
390 }
391 
392 print '</table>';
393 print '</div>';
394 
395 print '</div><div class="fichetwothirdright">';
396 
397 
398 // Show graphs
399 print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
400 if ($mesg) {
401  print $mesg;
402 } else {
403  print $px1->show();
404  print "<br>\n";
405  print $px2->show();
406  print "<br>\n";
407  print $px3->show();
408 }
409 print '</td></tr></table>';
410 
411 
412 print '</div></div>';
413 print '<div style="clear:both"></div>';
414 
415 
416 print dol_get_fiche_end();
417 
418 // End of page
419 llxFooter();
420 $db->close();
DolGraph\getDefaultGraphSizeForStats
static getDefaultGraphSizeForStats($direction, $defaultsize='')
getDefaultGraphSizeForStats
Definition: dolgraph.class.php:1539
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
$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
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
FormCompany
Class to build HTML component for third parties management Only common components are here.
Definition: html.formcompany.class.php:40
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
FactureStats
Class to manage stats for invoices (customer and supplier)
Definition: facturestats.class.php:34
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
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
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:2845
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:6603