dolibarr  9.0.0
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 <http://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  var $boxcode="productdistribution";
33  var $boximg="object_product";
34  var $boxlabel="BoxProductDistribution";
35  var $depends = array("product|service","facture|propal|commande");
36 
40  public $db;
41 
42  var $param;
43 
44  var $info_box_head = array();
45  var $info_box_contents = array();
46 
47 
54  function __construct($db,$param)
55  {
56  global $user, $conf;
57 
58  $this->db=$db;
59 
60  $this->hidden = ! (
61  (! empty($conf->facture->enabled) && ! empty($user->rights->facture->lire))
62  || (! empty($conf->commande->enabled) && ! empty($user->rights->commande->lire))
63  || (! empty($conf->propal->enabled) && ! empty($user->rights->propale->lire))
64  );
65  }
66 
73  function loadBox($max=5)
74  {
75  global $conf, $user, $langs, $db;
76 
77  $this->max=$max;
78 
79  $refreshaction='refresh_'.$this->boxcode;
80 
81  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
82  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
83  include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
84 
85  $param_year='DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
86  $param_showinvoicenb='DOLUSERCOOKIE_box_'.$this->boxcode.'_showinvoicenb';
87  $param_showpropalnb='DOLUSERCOOKIE_box_'.$this->boxcode.'_showpropalnb';
88  $param_showordernb='DOLUSERCOOKIE_box_'.$this->boxcode.'_showordernb';
89  $autosetarray=preg_split("/[,;:]+/",GETPOST('DOL_AUTOSET_COOKIE'));
90  if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode,$autosetarray))
91  {
92  $year=GETPOST($param_year,'int');
93  $showinvoicenb=GETPOST($param_showinvoicenb,'alpha');
94  $showpropalnb=GETPOST($param_showpropalnb,'alpha');
95  $showordernb=GETPOST($param_showordernb,'alpha');
96  }
97  else
98  {
99  $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true);
100  $year=$tmparray['year'];
101  $showinvoicenb=$tmparray['showinvoicenb'];
102  $showpropalnb=$tmparray['showpropalnb'];
103  $showordernb=$tmparray['showordernb'];
104  }
105  if (empty($showinvoicenb) && empty($showpropalnb) && empty($showordernb)) { $showpropalnb=1; $showinvoicenb=1; $showordernb=1; }
106  if (empty($conf->facture->enabled) || empty($user->rights->facture->lire)) $showinvoicenb=0;
107  if (empty($conf->propal->enabled) || empty($user->rights->propale->lire)) $showpropalnb=0;
108  if (empty($conf->commande->enabled) || empty($user->rights->commande->lire)) $showordernb=0;
109 
110  $nowarray=dol_getdate(dol_now(),true);
111  if (empty($year)) $year=$nowarray['year'];
112 
113  $nbofgraph=0;
114  if ($showinvoicenb) $nbofgraph++;
115  if ($showpropalnb) $nbofgraph++;
116  if ($showordernb) $nbofgraph++;
117 
118  $text = $langs->trans("BoxProductDistribution",$max).' - '.$langs->trans("Year").': '.$year;
119  $this->info_box_head = array(
120  'text' => $text,
121  'limit'=> dol_strlen($text),
122  'graph'=> 1,
123  'sublink'=>'',
124  'subtext'=>$langs->trans("Filter"),
125  'subpicto'=>'filter.png',
126  'subclass'=>'linkobject boxfilter',
127  'target'=>'none' // Set '' to get target="_blank"
128  );
129 
130 
131  $paramtitle=$langs->transnoentitiesnoconv("Products").'/'.$langs->transnoentitiesnoconv("Services");
132  if (empty($conf->produit->enabled)) $paramtitle=$langs->transnoentitiesnoconv("Services");
133  if (empty($conf->service->enabled)) $paramtitle=$langs->transnoentitiesnoconv("Products");
134 
135  $socid=empty($user->societe_id)?0:$user->societe_id;
136  $userid=0; // No filter on user creation
137 
138  $WIDTH=($nbofgraph >= 2 || ! empty($conf->dol_optimize_smallscreen))?'160':'320';
139  $HEIGHT='192';
140 
141  if (! empty($conf->facture->enabled) && ! empty($user->rights->facture->lire))
142  {
143  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
144  if ($showinvoicenb)
145  {
146  $langs->load("bills");
147  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
148 
149  $showpointvalue = 1; $nocolor = 0;
150  $mode='customer';
151  $stats_invoice = new FactureStats($this->db, $socid, $mode, ($userid>0?$userid:0));
152  $data1 = $stats_invoice->getAllByProductEntry($year,(GETPOST('action','aZ09')==$refreshaction?-1:(3600*24)));
153  if (empty($data1))
154  {
155  $showpointvalue=0;
156  $nocolor=1;
157  $data1=array(array(0=>$langs->trans("None"),1=>1));
158  }
159  $filenamenb = $dir."/prodserforinvoice-".$year.".png";
160  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&amp;file=prodserforinvoice-'.$year.'.png';
161 
162  $px1 = new DolGraph();
163  $mesg = $px1->isGraphKo();
164  if (! $mesg)
165  {
166  $i=0;$tot=count($data1);$legend=array();
167  while ($i <= $tot)
168  {
169  $data1[$i][0]=dol_trunc($data1[$i][0],5); // Required to avoid error "Could not draw pie with labels contained inside canvas"
170  $legend[]=$data1[$i][0];
171  $i++;
172  }
173 
174  $px1->SetData($data1);
175  unset($data1);
176 
177  if ($nocolor) $px1->SetDataColor(array(array(220,220,220)));
178  $px1->SetPrecisionY(0);
179  $px1->SetLegend($legend);
180  $px1->setShowLegend(0);
181  $px1->setShowPointValue($showpointvalue);
182  $px1->setShowPercent(0);
183  $px1->SetMaxValue($px1->GetCeilMaxValue());
184  $px1->SetWidth($WIDTH);
185  $px1->SetHeight($HEIGHT);
186  //$px1->SetYLabel($langs->trans("NumberOfBills"));
187  $px1->SetShading(3);
188  $px1->SetHorizTickIncrement(1);
189  $px1->SetPrecisionY(0);
190  $px1->SetCssPrefix("cssboxes");
191  //$px1->mode='depth';
192  $px1->SetType(array('pie'));
193  $px1->SetTitle($langs->trans("BoxProductDistributionFor",$paramtitle,$langs->transnoentitiesnoconv("Invoices")));
194  $px1->combine = 0.05;
195 
196  $px1->draw($filenamenb,$fileurlnb);
197  }
198  }
199  }
200 
201  if (! empty($conf->propal->enabled) && ! empty($user->rights->propale->lire))
202  {
203  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
204  if ($showpropalnb)
205  {
206  $langs->load("propal");
207  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propalestats.class.php';
208 
209  $showpointvalue = 1; $nocolor = 0;
210  $stats_proposal = new PropaleStats($this->db, $socid, ($userid>0?$userid:0));
211  $data2 = $stats_proposal->getAllByProductEntry($year,(GETPOST('action','aZ09')==$refreshaction?-1:(3600*24)));
212  if (empty($data2))
213  {
214  $showpointvalue = 0;
215  $nocolor = 1;
216  $data2=array(array(0=>$langs->trans("None"),1=>1));
217  }
218 
219  $filenamenb = $dir."/prodserforpropal-".$year.".png";
220  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=proposalstats&amp;file=prodserforpropal-'.$year.'.png';
221 
222  $px2 = new DolGraph();
223  $mesg = $px2->isGraphKo();
224  if (! $mesg)
225  {
226  $i=0;$tot=count($data2);$legend=array();
227  while ($i <= $tot)
228  {
229  $data2[$i][0]=dol_trunc($data2[$i][0],5); // Required to avoid error "Could not draw pie with labels contained inside canvas"
230  $legend[]=$data2[$i][0];
231  $i++;
232  }
233 
234  $px2->SetData($data2);
235  unset($data2);
236 
237  if ($nocolor) $px2->SetDataColor(array(array(220,220,220)));
238  $px2->SetPrecisionY(0);
239  $px2->SetLegend($legend);
240  $px2->setShowLegend(0);
241  $px2->setShowPointValue($showpointvalue);
242  $px2->setShowPercent(0);
243  $px2->SetMaxValue($px2->GetCeilMaxValue());
244  $px2->SetWidth($WIDTH);
245  $px2->SetHeight($HEIGHT);
246  //$px2->SetYLabel($langs->trans("AmountOfBillsHT"));
247  $px2->SetShading(3);
248  $px2->SetHorizTickIncrement(1);
249  $px2->SetPrecisionY(0);
250  $px2->SetCssPrefix("cssboxes");
251  //$px2->mode='depth';
252  $px2->SetType(array('pie'));
253  $px2->SetTitle($langs->trans("BoxProductDistributionFor",$paramtitle,$langs->transnoentitiesnoconv("Proposals")));
254  $px2->combine = 0.05;
255 
256  $px2->draw($filenamenb,$fileurlnb);
257  }
258  }
259  }
260 
261  if (! empty($conf->commande->enabled) && ! empty($user->rights->commande->lire))
262  {
263  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
264  if ($showordernb)
265  {
266  $langs->load("orders");
267  include_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
268 
269  $showpointvalue = 1; $nocolor = 0;
270  $mode='customer';
271  $stats_order = new CommandeStats($this->db, $socid, $mode, ($userid>0?$userid:0));
272  $data3 = $stats_order->getAllByProductEntry($year,(GETPOST('action','aZ09')==$refreshaction?-1:(3600*24)));
273  if (empty($data3))
274  {
275  $showpointvalue = 0;
276  $nocolor = 1;
277  $data3=array(array(0=>$langs->trans("None"),1=>1));
278  }
279 
280  $filenamenb = $dir."/prodserfororder-".$year.".png";
281  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&amp;file=prodserfororder-'.$year.'.png';
282 
283  $px3 = new DolGraph();
284  $mesg = $px3->isGraphKo();
285  if (! $mesg)
286  {
287  $i=0;$tot=count($data3);$legend=array();
288  while ($i <= $tot)
289  {
290  $data3[$i][0]=dol_trunc($data3[$i][0],5); // Required to avoid error "Could not draw pie with labels contained inside canvas"
291  $legend[]=$data3[$i][0];
292  $i++;
293  }
294 
295  $px3->SetData($data3);
296  unset($data3);
297 
298  if ($nocolor) $px3->SetDataColor(array(array(220,220,220)));
299  $px3->SetPrecisionY(0);
300  $px3->SetLegend($legend);
301  $px3->setShowLegend(0);
302  $px3->setShowPointValue($showpointvalue);
303  $px3->setShowPercent(0);
304  $px3->SetMaxValue($px3->GetCeilMaxValue());
305  $px3->SetWidth($WIDTH);
306  $px3->SetHeight($HEIGHT);
307  //$px3->SetYLabel($langs->trans("AmountOfBillsHT"));
308  $px3->SetShading(3);
309  $px3->SetHorizTickIncrement(1);
310  $px3->SetPrecisionY(0);
311  $px3->SetCssPrefix("cssboxes");
312  //$px3->mode='depth';
313  $px3->SetType(array('pie'));
314  $px3->SetTitle($langs->trans("BoxProductDistributionFor",$paramtitle,$langs->transnoentitiesnoconv("Orders")));
315  $px3->combine = 0.05;
316 
317  $px3->draw($filenamenb,$fileurlnb);
318  }
319  }
320  }
321 
322  if (empty($nbofgraph))
323  {
324  $langs->load("errors");
325  $mesg=$langs->trans("ReadPermissionNotAllowed");
326  }
327  if (empty($conf->use_javascript_ajax))
328  {
329  $langs->load("errors");
330  $mesg=$langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
331  }
332 
333  if (! $mesg)
334  {
335  $stringtoshow='';
336  $stringtoshow.='<script type="text/javascript" language="javascript">
337  jQuery(document).ready(function() {
338  jQuery("#idsubimg'.$this->boxcode.'").click(function() {
339  jQuery("#idfilter'.$this->boxcode.'").toggle();
340  });
341  });
342  </script>';
343  $stringtoshow.='<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
344  $stringtoshow.='<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
345  $stringtoshow.='<input type="hidden" name="action" value="'.$refreshaction.'">';
346  $stringtoshow.='<input type="hidden" name="page_y" value="">';
347  $stringtoshow.='<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,showinvoicenb,showpropalnb,showordernb">';
348  if (! empty($conf->facture->enabled) || ! empty($user->rights->facture->lire))
349  {
350  $stringtoshow.='<input type="checkbox" name="'.$param_showinvoicenb.'"'.($showinvoicenb?' checked':'').'> '.$langs->trans("ForCustomersInvoices");
351  $stringtoshow.=' &nbsp; ';
352  }
353  if (! empty($conf->propal->enabled) || ! empty($user->rights->propale->lire))
354  {
355  $stringtoshow.='<input type="checkbox" name="'.$param_showpropalnb.'"'.($showpropalnb?' checked':'').'> '.$langs->trans("ForProposals");
356  $stringtoshow.='&nbsp;';
357  }
358  if (! empty($conf->commande->enabled) || ! empty($user->rights->commande->lire))
359  {
360  $stringtoshow.='<input type="checkbox" name="'.$param_showordernb.'"'.($showordernb?' checked':'').'> '.$langs->trans("ForCustomersOrders");
361  }
362  $stringtoshow.='<br>';
363  $stringtoshow.=$langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$year.'">';
364  $stringtoshow.='<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto('','refresh.png','','',1).'">';
365  $stringtoshow.='</form>';
366  $stringtoshow.='</div>';
367 
368  if ($nbofgraph == 1)
369  {
370  if ($showinvoicenb) $stringtoshow.=$px1->show();
371  else if ($showpropalnb) $stringtoshow.=$px2->show();
372  else $stringtoshow.=$px3->show();
373  }
374  if ($nbofgraph == 2)
375  {
376  $stringtoshow.='<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
377  if ($showinvoicenb) $stringtoshow.=$px1->show();
378  else if ($showpropalnb) $stringtoshow.=$px2->show();
379  $stringtoshow.='</div><div class="fichehalfright">';
380  if ($showordernb) $stringtoshow.=$px3->show();
381  else if ($showpropalnb) $stringtoshow.=$px2->show();
382  $stringtoshow.='</div></div></div>';
383  }
384  if ($nbofgraph == 3)
385  {
386  $stringtoshow.='<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
387  $stringtoshow.=$px1->show();
388  $stringtoshow.='</div><div class="fichehalfright">';
389  $stringtoshow.=$px2->show();
390  $stringtoshow.='</div></div></div>';
391  $stringtoshow.='<div class="fichecenter"><div class="containercenter">';
392  $stringtoshow.=$px3->show();
393  $stringtoshow.='</div></div>';
394  }
395  $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'align="center" class="nohover"','textnoformat'=>$stringtoshow);
396  }
397  else
398  {
399  $this->info_box_contents[0][0] = array(
400  'td' => 'align="left" class="nohover opacitymedium"',
401  'maxlength'=>500,
402  'text' => $mesg
403  );
404  }
405  }
406 
415  function showBox($head = null, $contents = null, $nooutput=0)
416  {
417  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
418  }
419 }
420 
GETPOST($paramname, $check='none', $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 &#39;...&#39; if string larger than length.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Class to manage the box to show last invoices.
Class ModeleBoxes.
loadBox($max=5)
Load data into info_box_contents array to show array later.
dol_now($mode='gmt')
Return date for now.
Class to manage order statistics (customer and supplier)
Class to build graphs.
dol_getdate($timestamp, $fast=false)
Return an array with locale date info.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Class to manage stats for invoices (customer and supplier)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Class to manage proposals statistics.