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