dolibarr  13.0.2
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 (!empty($conf->category->enabled)) require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
36 
38 $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
39 
40 // Load translation files required by the page
41 $langs->loadLangs(array('bills', 'companies', 'other'));
42 
43 $mode = GETPOST("mode") ?GETPOST("mode") : 'customer';
44 if ($mode == 'customer' && !$user->rights->facture->lire) accessforbidden();
45 if ($mode == 'supplier' && !$user->rights->fournisseur->facture->lire) accessforbidden();
46 
47 $object_status = GETPOST('object_status', 'intcomma');
48 $typent_id = GETPOST('typent_id', 'int');
49 $categ_id = GETPOST('categ_id', 'categ_id');
50 
51 $userid = GETPOST('userid', 'int');
52 $socid = GETPOST('socid', 'int');
53 $custcats = GETPOST('custcats', 'array');
54 // Security check
55 if ($user->socid > 0)
56 {
57  $action = '';
58  $socid = $user->socid;
59 }
60 
61 $nowyear = strftime("%Y", dol_now());
62 $year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
63 if (!empty($conf->global->INVOICE_STATS_GRAPHS_SHOW_2_YEARS)) $startyear = $year - 2;
64 else $startyear = $year - 1;
65 $endyear = $year;
66 
67 
68 /*
69  * View
70  */
71 if (!empty($conf->category->enabled)) $langs->load('categories');
72 $form = new Form($db);
73 $formcompany = new FormCompany($db);
74 $formother = new FormOther($db);
75 
76 llxHeader();
77 
78 $picto = 'bill';
79 $title = $langs->trans("BillsStatistics");
80 $dir = $conf->facture->dir_temp;
81 
82 if ($mode == 'supplier')
83 {
84  $picto = 'supplier_invoice';
85  $title = $langs->trans("BillsStatisticsSuppliers");
86  $dir = $conf->fournisseur->facture->dir_temp;
87 }
88 
89 
90 print load_fiche_titre($title, '', $picto);
91 
92 dol_mkdir($dir);
93 
94 $stats = new FactureStats($db, $socid, $mode, ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
95 if ($mode == 'customer')
96 {
97  if ($object_status != '' && $object_status >= 0) $stats->where .= ' AND f.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
98  if (is_array($custcats) && !empty($custcats)) {
99  $stats->from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cat ON (f.fk_soc = cat.fk_soc)';
100  $stats->where .= ' AND cat.fk_categorie IN ('.implode(',', $custcats).')';
101  }
102 }
103 if ($mode == 'supplier')
104 {
105  if ($object_status != '' && $object_status >= 0) $stats->where .= ' AND f.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
106 }
107 
108 // Build graphic number of object
109 // $data = array(array('Lib',val1,val2,val3),...)
110 $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
111 //var_dump($data);
112 
113 $filenamenb = $dir."/invoicesnbinyear-".$year.".png";
114 if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesnbinyear-'.$year.'.png';
115 if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicesnbinyear-'.$year.'.png';
116 
117 $px1 = new DolGraph();
118 $mesg = $px1->isGraphKo();
119 if (!$mesg)
120 {
121  $px1->SetData($data);
122  $i = $startyear; $legend = array();
123  while ($i <= $endyear)
124  {
125  $legend[] = $i;
126  $i++;
127  }
128  $px1->SetLegend($legend);
129  $px1->SetMaxValue($px1->GetCeilMaxValue());
130  $px1->SetWidth($WIDTH);
131  $px1->SetHeight($HEIGHT);
132  $px1->SetYLabel($langs->trans("NumberOfBills"));
133  $px1->SetShading(3);
134  $px1->SetHorizTickIncrement(1);
135  $px1->mode = 'depth';
136  $px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
137 
138  $px1->draw($filenamenb, $fileurlnb);
139 }
140 
141 // Build graphic amount of object
142 $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
143 //var_dump($data);
144 // $data = array(array('Lib',val1,val2,val3),...)
145 
146 $filenameamount = $dir."/invoicesamountinyear-".$year.".png";
147 if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesamountinyear-'.$year.'.png';
148 if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicesamountinyear-'.$year.'.png';
149 
150 $px2 = new DolGraph();
151 $mesg = $px2->isGraphKo();
152 if (!$mesg)
153 {
154  $px2->SetData($data);
155  $i = $startyear; $legend = array();
156  while ($i <= $endyear)
157  {
158  $legend[] = $i;
159  $i++;
160  }
161  $px2->SetLegend($legend);
162  $px2->SetMaxValue($px2->GetCeilMaxValue());
163  $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
164  $px2->SetWidth($WIDTH);
165  $px2->SetHeight($HEIGHT);
166  $px2->SetYLabel($langs->trans("AmountOfBills"));
167  $px2->SetShading(3);
168  $px2->SetHorizTickIncrement(1);
169  $px2->mode = 'depth';
170  $px2->SetTitle($langs->trans("AmountOfBillsByMonthHT"));
171 
172  $px2->draw($filenameamount, $fileurlamount);
173 }
174 
175 
176 $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
177 
178 if (!$user->rights->societe->client->voir || $user->socid)
179 {
180  $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
181  if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
182  if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
183 } else {
184  $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
185  if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
186  if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
187 }
188 
189 $px3 = new DolGraph();
190 $mesg = $px3->isGraphKo();
191 if (!$mesg)
192 {
193  $px3->SetData($data);
194  $i = $startyear; $legend = array();
195  while ($i <= $endyear)
196  {
197  $legend[] = $i;
198  $i++;
199  }
200  $px3->SetLegend($legend);
201  $px3->SetYLabel($langs->trans("AmountAverage"));
202  $px3->SetMaxValue($px3->GetCeilMaxValue());
203  $px3->SetMinValue($px3->GetFloorMinValue());
204  $px3->SetWidth($WIDTH);
205  $px3->SetHeight($HEIGHT);
206  $px3->SetShading(3);
207  $px3->SetHorizTickIncrement(1);
208  $px3->mode = 'depth';
209  $px3->SetTitle($langs->trans("AmountAverage"));
210 
211  $px3->draw($filename_avg, $fileurl_avg);
212 }
213 
214 
215 // Show array
216 $data = $stats->getAllByYear();
217 $arrayyears = array();
218 foreach ($data as $val) {
219  $arrayyears[$val['year']] = $val['year'];
220 }
221 if (!count($arrayyears)) $arrayyears[$nowyear] = $nowyear;
222 
223 
224 $h = 0;
225 $head = array();
226 $head[$h][0] = DOL_URL_ROOT.'/compta/facture/stats/index.php?mode='.$mode;
227 $head[$h][1] = $langs->trans("ByMonthYear");
228 $head[$h][2] = 'byyear';
229 $h++;
230 
231 if ($mode == 'customer') $type = 'invoice_stats';
232 if ($mode == 'supplier') $type = 'supplier_invoice_stats';
233 
234 complete_head_from_modules($conf, $langs, null, $head, $h, $type);
235 
236 print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
237 
238 // We use select_thirdparty_list instead of select_company so we can use $filter and share same code for customer and supplier.
239 $tmp_companies = $form->select_thirdparty_list($socid, 'socid', $filter, 1, 0, 0, array(), '', 1);
240 //Array passed as an argument to Form::selectarray to build a proper select input
241 $companies = array();
242 
243 foreach ($tmp_companies as $value) {
244  $companies[$value['key']] = $value['label'];
245 }
246 
247 print '<div class="fichecenter"><div class="fichethirdleft">';
248 
249 
250 // Show filter box
251 print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
252 print '<input type="hidden" name="token" value="'.newToken().'">';
253 print '<input type="hidden" name="mode" value="'.$mode.'">';
254 
255 print '<table class="noborder centpercent">';
256 print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
257 // Company
258 print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
259 if ($mode == 'customer') $filter = 's.client in (1,2,3)';
260 if ($mode == 'supplier') $filter = 's.fournisseur = 1';
261 print $form->selectarray('socid', $companies, $socid, 1, 0, 0, 'style="width: 95%"', 0, 0, 0, '', '', 1);
262 print '</td></tr>';
263 
264 // ThirdParty Type
265 print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
266 $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.
267 print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 0, 0, 0, '', 0, 0, 0, $sortparam_typent);
268 if ($user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
269 print '</td></tr>';
270 
271 // Category
272 if (!empty($conf->category->enabled)) {
273  if ($mode == 'customer')
274  {
275  $cat_type = Categorie::TYPE_CUSTOMER;
276  $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
277  }
278  if ($mode == 'supplier')
279  {
280  $cat_type = Categorie::TYPE_SUPPLIER;
281  $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
282  }
283  print '<tr><td>'.$cat_label.'</td><td>';
284  $cate_arbo = $form->select_all_categories(Categorie::TYPE_CUSTOMER, null, 'parent', null, null, 1);
285  print $form->multiselectarray('custcats', $cate_arbo, GETPOST('custcats', 'array'), null, null, null, null, "90%");
286  //print $formother->select_categories($cat_type, $categ_id, 'categ_id', true);
287  print '</td></tr>';
288 }
289 
290 // User
291 print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
292 print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
293 print '</td></tr>';
294 // Status
295 print '<tr><td class="left">'.$langs->trans("Status").'</td><td class="left">';
296 if ($mode == 'customer')
297 {
298  $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"));
299  print $form->selectarray('object_status', $liststatus, $object_status, 1);
300 }
301 if ($mode == 'supplier')
302 {
303  $liststatus = array('0'=>$langs->trans("BillStatusDraft"), '1'=>$langs->trans("BillStatusNotPaid"), '2'=>$langs->trans("BillStatusPaid"));
304  print $form->selectarray('object_status', $liststatus, $object_status, 1);
305 }
306 print '</td></tr>';
307 // Year
308 print '<tr><td>'.$langs->trans("Year").'</td><td>';
309 if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year;
310 if (!in_array($nowyear, $arrayyears)) $arrayyears[$nowyear] = $nowyear;
311 arsort($arrayyears);
312 print $form->selectarray('year', $arrayyears, $year, 0);
313 print '</td></tr>';
314 print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
315 print '</table>';
316 print '</form>';
317 print '<br><br>';
318 
319 print '<div class="div-table-responsive-no-min">';
320 print '<table class="noborder centpercent">';
321 print '<tr class="liste_titre" height="24">';
322 print '<td class="center">'.$langs->trans("Year").'</td>';
323 print '<td class="right">'.$langs->trans("NumberOfBills").'</td>';
324 print '<td class="right">%</td>';
325 print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
326 print '<td class="right">%</td>';
327 print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
328 print '<td class="right">%</td>';
329 print '</tr>';
330 
331 $oldyear = 0;
332 foreach ($data as $val)
333 {
334  $year = $val['year'];
335  while ($year && $oldyear > $year + 1)
336  { // If we have empty year
337  $oldyear--;
338 
339  print '<tr class="oddeven" height="24">';
340  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>';
341  print '<td class="right">0</td>';
342  print '<td class="right"></td>';
343  print '<td class="right">0</td>';
344  print '<td class="right"></td>';
345  print '<td class="right">0</td>';
346  print '<td class="right"></td>';
347  print '</tr>';
348  }
349 
350  print '<tr class="oddeven" height="24">';
351  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>';
352  print '<td class="right">'.$val['nb'].'</td>';
353  print '<td class="right" style="'.(($val['nb_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['nb_diff']).'</td>';
354  print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
355  print '<td class="right" style="'.(($val['total_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['total_diff']).'</td>';
356  print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
357  print '<td class="right" style="'.(($val['avg_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['avg_diff']).'</td>';
358  print '</tr>';
359  $oldyear = $year;
360 }
361 
362 print '</table>';
363 print '</div>';
364 
365 print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
366 
367 
368 // Show graphs
369 print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
370 if ($mesg) { print $mesg; } else {
371  print $px1->show();
372  print "<br>\n";
373  print $px2->show();
374  print "<br>\n";
375  print $px3->show();
376 }
377 print '</td></tr></table>';
378 
379 
380 print '</div></div></div>';
381 print '<div style="clear:both"></div>';
382 
383 
385 
386 // End of page
387 llxFooter();
388 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_now($mode= 'auto')
Return date for now.
llxHeader()
Empty header.
Definition: wrapper.php:45
Class to build HTML component for third parties management Only common components are here...
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Classe permettant la generation de composants html autre Only common components are here...
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 ...
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
Class to build graphs.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
static getDefaultGraphSizeForStats($direction, $defaultsize= '')
getDefaultGraphSizeForStats
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode= 'add')
Complete or removed entries into a head array (used to build tabs).
llxFooter()
Empty footer.
Definition: wrapper.php:59
Class to manage stats for invoices (customer and supplier)
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)