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