dolibarr  13.0.2
index.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
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.'/commande/class/commande.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formorder.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
37 
39 $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
40 
41 $mode = GETPOSTISSET("mode") ? GETPOST("mode", 'aZ09') : 'customer';
42 if ($mode == 'customer' && !$user->rights->commande->lire) accessforbidden();
43 if ($mode == 'supplier' && !$user->rights->fournisseur->commande->lire) accessforbidden();
44 
45 $object_status = GETPOST('object_status', 'intcomma');
46 $typent_id = GETPOST('typent_id', 'int');
47 $categ_id = GETPOST('categ_id', 'categ_id');
48 
49 $userid = GETPOST('userid', 'int');
50 $socid = GETPOST('socid', 'int');
51 // Security check
52 if ($user->socid > 0)
53 {
54  $action = '';
55  $socid = $user->socid;
56 }
57 
58 $nowyear = strftime("%Y", dol_now());
59 $year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
60 //$startyear=$year-2;
61 $startyear = $year - 1;
62 $endyear = $year;
63 
64 // Load translation files required by the page
65 $langs->loadLangs(array('orders', 'companies', 'other', 'suppliers'));
66 
67 
68 /*
69  * View
70  */
71 
72 $form = new Form($db);
73 $formorder = new FormOrder($db);
74 $formcompany = new FormCompany($db);
75 $formother = new FormOther($db);
76 
77 $picto = 'order';
78 $title = $langs->trans("OrdersStatistics");
79 $dir = $conf->commande->dir_temp;
80 
81 if ($mode == 'supplier')
82 {
83  $picto = 'supplier_order';
84  $title = $langs->trans("OrdersStatisticsSuppliers");
85  $dir = $conf->fournisseur->commande->dir_temp;
86 }
87 
88 llxHeader('', $title);
89 
90 print load_fiche_titre($title, '', $picto);
91 
92 dol_mkdir($dir);
93 
94 $stats = new CommandeStats($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 >= -1) $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
98 }
99 if ($mode == 'supplier')
100 {
101  if ($object_status != '' && $object_status >= 0) $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
102 }
103 
104 
105 // Build graphic number of object
106 $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
107 
108 //var_dump($data);
109 // $data = array(array('Lib',val1,val2,val3),...)
110 
111 
112 if (!$user->rights->societe->client->voir || $user->socid)
113 {
114  $filenamenb = $dir.'/ordersnbinyear-'.$user->id.'-'.$year.'.png';
115  if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
116  if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
117 } else {
118  $filenamenb = $dir.'/ordersnbinyear-'.$year.'.png';
119  if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$year.'.png';
120  if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$year.'.png';
121 }
122 
123 $px1 = new DolGraph();
124 $mesg = $px1->isGraphKo();
125 if (!$mesg)
126 {
127  $px1->SetData($data);
128  $i = $startyear; $legend = array();
129  while ($i <= $endyear)
130  {
131  $legend[] = $i;
132  $i++;
133  }
134  $px1->SetLegend($legend);
135  $px1->SetMaxValue($px1->GetCeilMaxValue());
136  $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
137  $px1->SetWidth($WIDTH);
138  $px1->SetHeight($HEIGHT);
139  $px1->SetYLabel($langs->trans("NbOfOrder"));
140  $px1->SetShading(3);
141  $px1->SetHorizTickIncrement(1);
142  $px1->mode = 'depth';
143  $px1->SetTitle($langs->trans("NumberOfOrdersByMonth"));
144 
145  $px1->draw($filenamenb, $fileurlnb);
146 }
147 
148 // Build graphic amount of object
149 $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
150 //var_dump($data);
151 // $data = array(array('Lib',val1,val2,val3),...)
152 
153 if (!$user->rights->societe->client->voir || $user->socid)
154 {
155  $filenameamount = $dir.'/ordersamountinyear-'.$user->id.'-'.$year.'.png';
156  if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
157  if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
158 } else {
159  $filenameamount = $dir.'/ordersamountinyear-'.$year.'.png';
160  if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$year.'.png';
161  if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$year.'.png';
162 }
163 
164 $px2 = new DolGraph();
165 $mesg = $px2->isGraphKo();
166 if (!$mesg)
167 {
168  $px2->SetData($data);
169  $i = $startyear; $legend = array();
170  while ($i <= $endyear)
171  {
172  $legend[] = $i;
173  $i++;
174  }
175  $px2->SetLegend($legend);
176  $px2->SetMaxValue($px2->GetCeilMaxValue());
177  $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
178  $px2->SetWidth($WIDTH);
179  $px2->SetHeight($HEIGHT);
180  $px2->SetYLabel($langs->trans("AmountOfOrders"));
181  $px2->SetShading(3);
182  $px2->SetHorizTickIncrement(1);
183  $px2->mode = 'depth';
184  $px2->SetTitle($langs->trans("AmountOfOrdersByMonthHT"));
185 
186  $px2->draw($filenameamount, $fileurlamount);
187 }
188 
189 
190 $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
191 
192 if (!$user->rights->societe->client->voir || $user->socid)
193 {
194  $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
195  if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
196  if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
197 } else {
198  $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
199  if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
200  if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
201 }
202 
203 $px3 = new DolGraph();
204 $mesg = $px3->isGraphKo();
205 if (!$mesg)
206 {
207  $px3->SetData($data);
208  $i = $startyear; $legend = array();
209  while ($i <= $endyear)
210  {
211  $legend[] = $i;
212  $i++;
213  }
214  $px3->SetLegend($legend);
215  $px3->SetYLabel($langs->trans("AmountAverage"));
216  $px3->SetMaxValue($px3->GetCeilMaxValue());
217  $px3->SetMinValue($px3->GetFloorMinValue());
218  $px3->SetWidth($WIDTH);
219  $px3->SetHeight($HEIGHT);
220  $px3->SetShading(3);
221  $px3->SetHorizTickIncrement(1);
222  $px3->mode = 'depth';
223  $px3->SetTitle($langs->trans("AmountAverage"));
224 
225  $px3->draw($filename_avg, $fileurl_avg);
226 }
227 
228 
229 
230 // Show array
231 $data = $stats->getAllByYear();
232 $arrayyears = array();
233 foreach ($data as $val) {
234  if (!empty($val['year'])) {
235  $arrayyears[$val['year']] = $val['year'];
236  }
237 }
238 if (!count($arrayyears)) $arrayyears[$nowyear] = $nowyear;
239 
240 $h = 0;
241 $head = array();
242 $head[$h][0] = DOL_URL_ROOT.'/commande/stats/index.php?mode='.$mode;
243 $head[$h][1] = $langs->trans("ByMonthYear");
244 $head[$h][2] = 'byyear';
245 $h++;
246 
247 if ($mode == 'customer') $type = 'order_stats';
248 if ($mode == 'supplier') $type = 'supplier_order_stats';
249 
250 complete_head_from_modules($conf, $langs, null, $head, $h, $type);
251 
252 print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
253 
254 
255 print '<div class="fichecenter"><div class="fichethirdleft">';
256 
257 
258 // Show filter box
259 print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
260 print '<input type="hidden" name="token" value="'.newToken().'">';
261 print '<input type="hidden" name="mode" value="'.$mode.'">';
262 
263 print '<table class="noborder centpercent">';
264 print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
265 // Company
266 print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
267 if ($mode == 'customer') $filter = 's.client IN (1,2,3)';
268 if ($mode == 'supplier') $filter = 's.fournisseur = 1';
269 print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, '', 'style="width: 95%"');
270 print '</td></tr>';
271 // ThirdParty Type
272 print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
273 $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.
274 print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 0, 0, 0, '', 0, 0, 0, $sortparam_typent);
275 if ($user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
276 print '</td></tr>';
277 // Category
278 if ($mode == 'customer')
279 {
280  $cat_type = Categorie::TYPE_CUSTOMER;
281  $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
282 }
283 if ($mode == 'supplier')
284 {
285  $cat_type = Categorie::TYPE_SUPPLIER;
286  $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
287 }
288 print '<tr><td>'.$cat_label.'</td><td>';
289 print $formother->select_categories($cat_type, $categ_id, 'categ_id', true);
290 print '</td></tr>';
291 // User
292 print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
293 print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
294 // Status
295 print '<tr><td class="left">'.$langs->trans("Status").'</td><td class="left">';
296 if ($mode == 'customer')
297 {
298  $liststatus = array(
299  Commande::STATUS_DRAFT=>$langs->trans("StatusOrderDraft"),
300  Commande::STATUS_VALIDATED=>$langs->trans("StatusOrderValidated"),
301  Commande::STATUS_SHIPMENTONPROCESS=>$langs->trans("StatusOrderSent"),
302  Commande::STATUS_CLOSED=>$langs->trans("StatusOrderDelivered"),
303  Commande::STATUS_CANCELED=>$langs->trans("StatusOrderCanceled")
304  );
305  print $form->selectarray('object_status', $liststatus, GETPOST('object_status', 'intcomma'), -4);
306 }
307 if ($mode == 'supplier')
308 {
309  $formorder->selectSupplierOrderStatus((strstr($object_status, ',') ? -1 : $object_status), 0, 'object_status');
310 }
311 print '</td></tr>';
312 // Year
313 print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
314 if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year;
315 if (!in_array($nowyear, $arrayyears)) $arrayyears[$nowyear] = $nowyear;
316 arsort($arrayyears);
317 print $form->selectarray('year', $arrayyears, $year, 0);
318 print '</td></tr>';
319 print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
320 print '</table>';
321 print '</form>';
322 print '<br><br>';
323 
324 
325 print '<div class="div-table-responsive-no-min">';
326 print '<table class="noborder centpercent">';
327 print '<tr class="liste_titre" height="24">';
328 print '<td class="center">'.$langs->trans("Year").'</td>';
329 print '<td class="right">'.$langs->trans("NbOfOrders").'</td>';
330 print '<td class="right">%</td>';
331 print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
332 print '<td class="right">%</td>';
333 print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
334 print '<td class="right">%</td>';
335 print '</tr>';
336 
337 $oldyear = 0;
338 foreach ($data as $val)
339 {
340  $year = $val['year'];
341  while (!empty($year) && $oldyear > $year + 1)
342  { // If we have empty year
343  $oldyear--;
344 
345  print '<tr class="oddeven" height="24">';
346  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>';
347  print '<td class="right">0</td>';
348  print '<td class="right"></td>';
349  print '<td class="right">0</td>';
350  print '<td class="right"></td>';
351  print '<td class="right">0</td>';
352  print '<td class="right"></td>';
353  print '</tr>';
354  }
355 
356 
357  print '<tr class="oddeven" height="24">';
358  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>';
359  print '<td class="right">'.$val['nb'].'</td>';
360  print '<td class="right" style="'.(($val['nb_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['nb_diff']).'</td>';
361  print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
362  print '<td class="right" style="'.(($val['total_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['total_diff']).'</td>';
363  print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
364  print '<td class="right" style="'.(($val['avg_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.round($val['avg_diff']).'</td>';
365  print '</tr>';
366  $oldyear = $year;
367 }
368 
369 print '</table>';
370 print '</div>';
371 
372 
373 print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
374 
375 
376 // Show graphs
377 print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
378 if ($mesg) { print $mesg; } else {
379  print $px1->show();
380  print "<br>\n";
381  print $px2->show();
382  print "<br>\n";
383  print $px3->show();
384 }
385 print '</td></tr></table>';
386 
387 
388 print '</div></div></div>';
389 print '<div style="clear:both"></div>';
390 
391 print dol_get_fiche_end();
392 
393 // End of page
394 llxFooter();
395 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
const STATUS_CLOSED
Closed (Sent, billed or not)
dol_now($mode= 'auto')
Return date for now.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
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;...
Class to manage HTML output components for orders Before adding component here, check they are not in...
const STATUS_DRAFT
Draft status.
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 ...
const STATUS_VALIDATED
Validated status.
Class to manage order statistics (customer and supplier)
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
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
const STATUS_CANCELED
Canceled status.