dolibarr  19.0.0-dev
box_graph_product_distribution.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
25 include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
26 
31 {
32  public $boxcode = "productdistribution";
33  public $boximg = "object_product";
34  public $boxlabel = "BoxProductDistribution";
35  public $depends = array("product|service", "facture|propal|commande");
36 
40  public $db;
41 
42  public $param;
43 
44  public $info_box_head = array();
45  public $info_box_contents = array();
46 
47  public $widgettype = 'graph';
48 
49 
56  public function __construct($db, $param)
57  {
58  global $user, $conf;
59 
60  $this->db = $db;
61 
62  $this->hidden = !(
63  (isModEnabled('facture') && $user->hasRight('facture', 'lire'))
64  || (isModEnabled('commande') && $user->hasRight('commande', 'lire'))
65  || (isModEnabled('propal') && $user->hasRight('propal', 'lire'))
66  );
67  }
68 
75  public function loadBox($max = 5)
76  {
77  global $conf, $user, $langs;
78 
79  $this->max = $max;
80  $dir = $conf->user->dir_temp;
81 
82  $refreshaction = 'refresh_'.$this->boxcode;
83 
84  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
85  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
86  include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
87 
88  $param_year = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
89  $param_showinvoicenb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showinvoicenb';
90  $param_showpropalnb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showpropalnb';
91  $param_showordernb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showordernb';
92  $autosetarray = preg_split("/[,;:]+/", GETPOST('DOL_AUTOSET_COOKIE'));
93  if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode, $autosetarray)) {
94  $year = GETPOST($param_year, 'int');
95  $showinvoicenb = GETPOST($param_showinvoicenb, 'alpha');
96  $showpropalnb = GETPOST($param_showpropalnb, 'alpha');
97  $showordernb = GETPOST($param_showordernb, 'alpha');
98  } else {
99  $tmparray = (!empty($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode]) ? json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode], true) : array());
100  $year = (!empty($tmparray['year']) ? $tmparray['year'] : '');
101  $showinvoicenb = (!empty($tmparray['showinvoicenb']) ? $tmparray['showinvoicenb'] : '');
102  $showpropalnb = (!empty($tmparray['showpropalnb']) ? $tmparray['showpropalnb'] : '');
103  $showordernb = (!empty($tmparray['showordernb']) ? $tmparray['showordernb'] : '');
104  }
105  if (empty($showinvoicenb) && empty($showpropalnb) && empty($showordernb)) {
106  $showpropalnb = 1;
107  $showinvoicenb = 1;
108  $showordernb = 1;
109  }
110  if (!isModEnabled('facture') || !$user->hasRight('facture', 'lire')) {
111  $showinvoicenb = 0;
112  }
113  if (isModEnabled('propal') || empty($user->rights->propal->lire)) {
114  $showpropalnb = 0;
115  }
116  if (!isModEnabled('commande') || empty($user->rights->commande->lire)) {
117  $showordernb = 0;
118  }
119 
120  $nowarray = dol_getdate(dol_now(), true);
121  if (empty($year)) {
122  $year = $nowarray['year'];
123  }
124 
125  $nbofgraph = 0;
126  if ($showinvoicenb) {
127  $nbofgraph++;
128  }
129  if ($showpropalnb) {
130  $nbofgraph++;
131  }
132  if ($showordernb) {
133  $nbofgraph++;
134  }
135 
136  $text = $langs->trans("BoxProductDistribution", $max).' - '.$langs->trans("Year").': '.$year;
137  $this->info_box_head = array(
138  'text' => $text,
139  'limit'=> dol_strlen($text),
140  'graph'=> 1,
141  'sublink'=>'',
142  'subtext'=>$langs->trans("Filter"),
143  'subpicto'=>'filter.png',
144  'subclass'=>'linkobject boxfilter',
145  'target'=>'none' // Set '' to get target="_blank"
146  );
147 
148 
149  $socid = empty($user->socid) ? 0 : $user->socid;
150  $userid = 0; // No filter on user creation
151 
152  $WIDTH = ($nbofgraph >= 2 || !empty($conf->dol_optimize_smallscreen)) ? '300' : '320';
153  $HEIGHT = '150'; // Height require to have 5+1 entries into legend visible.
154 
155  if (isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
156  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
157  if ($showpropalnb) {
158  $langs->load("propal");
159  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propalestats.class.php';
160 
161  $showpointvalue = 1;
162  $nocolor = 0;
163  $stats_proposal = new PropaleStats($this->db, $socid, ($userid > 0 ? $userid : 0));
164  $data2 = $stats_proposal->getAllByProductEntry($year, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), $max);
165  if (empty($data2)) {
166  $showpointvalue = 0;
167  $nocolor = 1;
168  $data2 = array(array(0=>$langs->trans("None"), 1=>1));
169  }
170 
171  $filenamenb = $dir."/prodserforpropal-".$year.".png";
172  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=proposalstats&amp;file=prodserforpropal-'.$year.'.png';
173 
174  $px2 = new DolGraph();
175  $mesg = $px2->isGraphKo();
176  if (!$mesg) {
177  $i = 0;
178  $legend = array();
179 
180  // Truncate length of legend
181  foreach ($data2 as $key => $val) {
182  $data2[$key][0] = dol_trunc($data2[$key][0], 32);
183  $legend[] = $data2[$key][0];
184  $i++;
185  }
186 
187  $px2->SetData($data2);
188  unset($data2);
189 
190  if ($nocolor) {
191  $px2->SetDataColor(array(array(220, 220, 220)));
192  }
193  $px2->SetLegend($legend);
194  $px2->setShowLegend(2);
195  if (!empty($conf->dol_optimize_smallscreen)) {
196  $px2->SetWidth(320);
197  }
198  $px2->setShowPointValue($showpointvalue);
199  $px2->setShowPercent(0);
200  $px2->SetMaxValue($px2->GetCeilMaxValue());
201  $px2->SetWidth($WIDTH);
202  $px2->SetHeight($HEIGHT);
203  //$px2->SetYLabel($langs->trans("AmountOfBillsHT"));
204  $px2->SetShading(3);
205  $px2->SetHorizTickIncrement(1);
206  $px2->SetCssPrefix("cssboxes");
207  //$px2->mode='depth';
208  $px2->SetType(array('pie'));
209  $px2->SetTitle($langs->trans("ForObject", $langs->transnoentitiesnoconv("Proposals")));
210  $px2->combine = 0.05;
211 
212  $px2->draw($filenamenb, $fileurlnb);
213  }
214  }
215  }
216 
217  if (isModEnabled('commande') && $user->hasRight('commande', 'lire')) {
218  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
219  if ($showordernb) {
220  $langs->load("orders");
221  include_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
222 
223  $showpointvalue = 1;
224  $nocolor = 0;
225  $mode = 'customer';
226  $stats_order = new CommandeStats($this->db, $socid, $mode, ($userid > 0 ? $userid : 0));
227  $data3 = $stats_order->getAllByProductEntry($year, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), $max);
228  if (empty($data3)) {
229  $showpointvalue = 0;
230  $nocolor = 1;
231  $data3 = array(array(0=>$langs->trans("None"), 1=>1));
232  }
233 
234  $filenamenb = $dir."/prodserfororder-".$year.".png";
235  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&amp;file=prodserfororder-'.$year.'.png';
236 
237  $px3 = new DolGraph();
238  $mesg = $px3->isGraphKo();
239  if (!$mesg) {
240  $i = 0;
241  $legend = array();
242 
243  // Truncate length of legend
244  foreach ($data3 as $key => $val) {
245  $data3[$key][0] = dol_trunc($data3[$key][0], 32);
246  $legend[] = $data3[$key][0];
247  $i++;
248  }
249 
250  $px3->SetData($data3);
251  unset($data3);
252 
253  if ($nocolor) {
254  $px3->SetDataColor(array(array(220, 220, 220)));
255  }
256  $px3->SetLegend($legend);
257  $px3->setShowLegend(2);
258  if (!empty($conf->dol_optimize_smallscreen)) {
259  $px3->SetWidth(320);
260  }
261  $px3->setShowPointValue($showpointvalue);
262  $px3->setShowPercent(0);
263  $px3->SetMaxValue($px3->GetCeilMaxValue());
264  $px3->SetWidth($WIDTH);
265  $px3->SetHeight($HEIGHT);
266  //$px3->SetYLabel($langs->trans("AmountOfBillsHT"));
267  $px3->SetShading(3);
268  $px3->SetHorizTickIncrement(1);
269  $px3->SetCssPrefix("cssboxes");
270  //$px3->mode='depth';
271  $px3->SetType(array('pie'));
272  $px3->SetTitle($langs->trans("ForObject", $langs->transnoentitiesnoconv("Orders")));
273  $px3->combine = 0.05;
274 
275  $px3->draw($filenamenb, $fileurlnb);
276  }
277  }
278  }
279 
280 
281  if (isModEnabled('facture') && $user->hasRight('facture', 'lire')) {
282  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
283  if ($showinvoicenb) {
284  $langs->load("bills");
285  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
286 
287  $showpointvalue = 1;
288  $nocolor = 0;
289  $mode = 'customer';
290  $stats_invoice = new FactureStats($this->db, $socid, $mode, ($userid > 0 ? $userid : 0));
291  $data1 = $stats_invoice->getAllByProductEntry($year, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), $max);
292 
293  if (empty($data1)) {
294  $showpointvalue = 0;
295  $nocolor = 1;
296  $data1 = array(array(0=>$langs->trans("None"), 1=>1));
297  }
298  $filenamenb = $dir."/prodserforinvoice-".$year.".png";
299  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&amp;file=prodserforinvoice-'.$year.'.png';
300 
301  $px1 = new DolGraph();
302  $mesg = $px1->isGraphKo();
303  if (!$mesg) {
304  $i = 0;
305  $legend = array();
306 
307  // Truncate length of legend
308  foreach ($data1 as $key => $val) {
309  $data1[$key][0] = dol_trunc($data1[$key][0], 32);
310  $legend[] = $data1[$key][0];
311  $i++;
312  }
313 
314  $px1->SetData($data1);
315  unset($data1);
316 
317  if ($nocolor) {
318  $px1->SetDataColor(array(array(220, 220, 220)));
319  }
320  $px1->SetLegend($legend);
321  $px1->setShowLegend(2);
322  if (!empty($conf->dol_optimize_smallscreen)) {
323  $px1->SetWidth(320);
324  }
325  $px1->setShowPointValue($showpointvalue);
326  $px1->setShowPercent(0);
327  $px1->SetMaxValue($px1->GetCeilMaxValue());
328  $px1->SetWidth($WIDTH);
329  $px1->SetHeight($HEIGHT);
330  //$px1->SetYLabel($langs->trans("NumberOfBills"));
331  $px1->SetShading(3);
332  $px1->SetHorizTickIncrement(1);
333  $px1->SetCssPrefix("cssboxes");
334  //$px1->mode='depth';
335  $px1->SetType(array('pie'));
336  $px1->SetTitle($langs->trans("ForObject", $langs->transnoentitiesnoconv("Invoices")));
337  $px1->combine = 0.05;
338 
339  $px1->draw($filenamenb, $fileurlnb);
340  }
341  }
342  }
343 
344  if (empty($nbofgraph)) {
345  $langs->load("errors");
346  $mesg = $langs->trans("ReadPermissionNotAllowed");
347  }
348  if (empty($conf->use_javascript_ajax)) {
349  $langs->load("errors");
350  $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
351  }
352 
353  if (!$mesg) {
354  $stringtoshow = '';
355  $stringtoshow .= '<script nonce="'.getNonce().'" type="text/javascript">
356  jQuery(document).ready(function() {
357  jQuery("#idsubimg'.$this->boxcode.'").click(function() {
358  jQuery("#idfilter'.$this->boxcode.'").toggle();
359  });
360  });
361  </script>';
362  $stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
363  $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
364  $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
365  $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
366  $stringtoshow .= '<input type="hidden" name="page_y" value="">';
367  $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,showinvoicenb,showpropalnb,showordernb">';
368  if (isModEnabled("propal") || $user->hasRight('propal', 'lire')) {
369  $stringtoshow .= '<input type="checkbox" name="'.$param_showpropalnb.'"'.($showpropalnb ? ' checked' : '').'> '.$langs->trans("ForProposals");
370  $stringtoshow .= '&nbsp;';
371  }
372  if (isModEnabled('commande') || $user->hasRight('commande', 'lire')) {
373  $stringtoshow .= '<input type="checkbox" name="'.$param_showordernb.'"'.($showordernb ? ' checked' : '').'> '.$langs->trans("ForCustomersOrders");
374  }
375  if (isModEnabled('facture') || $user->hasRight('facture', 'lire')) {
376  $stringtoshow .= '<input type="checkbox" name="'.$param_showinvoicenb.'"'.($showinvoicenb ? ' checked' : '').'> '.$langs->trans("ForCustomersInvoices");
377  $stringtoshow .= ' &nbsp; ';
378  }
379  $stringtoshow .= '<br>';
380  $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$year.'">';
381  $stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto('', 'refresh.png', '', '', 1).'">';
382  $stringtoshow .= '</form>';
383  $stringtoshow .= '</div>';
384 
385  if ($nbofgraph == 1) {
386  if ($showpropalnb) {
387  $stringtoshow .= $px2->show();
388  } elseif ($showordernb) {
389  $stringtoshow .= $px3->show();
390  } else {
391  $stringtoshow .= $px1->show();
392  }
393  }
394  if ($nbofgraph == 2) {
395  $stringtoshow .= '<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
396  if ($showpropalnb) {
397  $stringtoshow .= $px2->show();
398  } elseif ($showordernb) {
399  $stringtoshow .= $px3->show();
400  }
401  $stringtoshow .= '</div><div class="fichehalfright">';
402  if ($showinvoicenb) {
403  $stringtoshow .= $px1->show();
404  } elseif ($showordernb) {
405  $stringtoshow .= $px3->show();
406  }
407  $stringtoshow .= '</div></div></div>';
408  }
409  if ($nbofgraph == 3) {
410  $stringtoshow .= '<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
411  $stringtoshow .= $px2->show();
412  $stringtoshow .= '</div><div class="fichehalfright">';
413  $stringtoshow .= $px3->show();
414  $stringtoshow .= '</div></div></div>';
415  $stringtoshow .= '<div class="fichecenter"><div class="containercenter">';
416  $stringtoshow .= $px1->show();
417  $stringtoshow .= '</div></div>';
418  }
419  $this->info_box_contents[0][0] = array(
420  'tr' => 'class="oddeven nohover"',
421  'td' => 'class="nohover center"',
422  'textnoformat'=>$stringtoshow,
423  );
424  } else {
425  $this->info_box_contents[0][0] = array(
426  'td' => 'class="nohover opacitymedium left"',
427  'maxlength'=>500,
428  'text' => $mesg
429  );
430  }
431  }
432 
441  public function showBox($head = null, $contents = null, $nooutput = 0)
442  {
443  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
444  }
445 }
Class to manage order statistics (customer and supplier)
Class to build graphs.
Class to manage stats for invoices (customer and supplier)
Class ModeleBoxes.
Class to manage proposals statistics.
Class to manage the box to show last invoices.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
loadBox($max=5)
Load data into info_box_contents array to show array later.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
isModEnabled($module)
Is Dolibarr module enabled.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.