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