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 $mesg = '';
141 $px1 = null;
142 $px2 = null;
143 $px3 = null;
144 $userid = 0; // No filter on user creation
145
146 $WIDTH = ($nbofgraph >= 2 || !empty($conf->dol_optimize_smallscreen)) ? '300' : '320';
147 $HEIGHT = '150'; // Height require to have 5+1 entries into legend visible.
148
149 if (isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
150 // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
151 if ($showpropalnb) {
152 $langs->load("propal");
153 include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propalestats.class.php';
154
155 $showpointvalue = 1;
156 $nocolor = 0;
157 $stats_proposal = new PropaleStats($this->db, $socid, ($userid > 0 ? $userid : 0));
158 $data2 = $stats_proposal->getAllByProductEntry($year, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), $max);
159 if (empty($data2)) {
160 $showpointvalue = 0;
161 $nocolor = 1;
162 $data2 = array(array(0 => $langs->trans("None"), 1 => 1));
163 }
164
165 $filenamenb = $dir."/prodserforpropal-".$year.".png";
166 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=proposalstats&amp;file=prodserforpropal-'.$year.'.png';
167
168 $px2 = new DolGraph();
169 $mesg = $px2->isGraphKo();
170 if (!$mesg) {
171 $i = 0;
172 $legend = array();
173
174 // Truncate length of legend
175 foreach ($data2 as $key => $val) {
176 $data2[$key][0] = dol_trunc($data2[$key][0], 32);
177 $legend[] = $data2[$key][0];
178 $i++;
179 }
180
181 $px2->SetData($data2);
182 unset($data2);
183
184 if ($nocolor) {
185 $px2->SetDataColor(array(array(220, 220, 220)));
186 }
187 $px2->SetLegend($legend);
188 $px2->setShowLegend(2);
189 if (!empty($conf->dol_optimize_smallscreen)) {
190 $px2->SetWidth(320);
191 }
192 $px2->setShowPointValue($showpointvalue);
193 $px2->setShowPercent(0);
194 $px2->SetMaxValue($px2->GetCeilMaxValue());
195 $px2->SetWidth($WIDTH);
196 $px2->SetHeight($HEIGHT);
197 //$px2->SetYLabel($langs->trans("AmountOfBillsHT"));
198 $px2->SetShading(3);
199 $px2->SetHorizTickIncrement(1);
200 $px2->SetCssPrefix("cssboxes");
201 //$px2->mode='depth';
202 $px2->SetType(array('pie'));
203 $px2->SetTitle($langs->trans("ForObject", $langs->transnoentitiesnoconv("Proposals")));
204 $px2->combine = 0.05;
205
206 $px2->draw($filenamenb, $fileurlnb);
207 }
208 }
209 }
210
211 if (isModEnabled('order') && $user->hasRight('commande', 'lire')) {
212 // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
213 if ($showordernb) {
214 $langs->load("orders");
215 include_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
216
217 $showpointvalue = 1;
218 $nocolor = 0;
219 $mode = 'customer';
220 $stats_order = new CommandeStats($this->db, $socid, $mode, ($userid > 0 ? $userid : 0));
221 $data3 = $stats_order->getAllByProductEntry($year, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), $max);
222 if (empty($data3)) {
223 $showpointvalue = 0;
224 $nocolor = 1;
225 $data3 = array(array(0 => $langs->trans("None"), 1 => 1));
226 }
227
228 $filenamenb = $dir."/prodserfororder-".$year.".png";
229 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&amp;file=prodserfororder-'.$year.'.png';
230
231 $px3 = new DolGraph();
232 $mesg = $px3->isGraphKo();
233 if (!$mesg) {
234 $i = 0;
235 $legend = array();
236
237 // Truncate length of legend
238 foreach ($data3 as $key => $val) {
239 $data3[$key][0] = dol_trunc($data3[$key][0], 32);
240 $legend[] = $data3[$key][0];
241 $i++;
242 }
243
244 $px3->SetData($data3);
245 unset($data3);
246
247 if ($nocolor) {
248 $px3->SetDataColor(array(array(220, 220, 220)));
249 }
250 $px3->SetLegend($legend);
251 $px3->setShowLegend(2);
252 if (!empty($conf->dol_optimize_smallscreen)) {
253 $px3->SetWidth(320);
254 }
255 $px3->setShowPointValue($showpointvalue);
256 $px3->setShowPercent(0);
257 $px3->SetMaxValue($px3->GetCeilMaxValue());
258 $px3->SetWidth($WIDTH);
259 $px3->SetHeight($HEIGHT);
260 //$px3->SetYLabel($langs->trans("AmountOfBillsHT"));
261 $px3->SetShading(3);
262 $px3->SetHorizTickIncrement(1);
263 $px3->SetCssPrefix("cssboxes");
264 //$px3->mode='depth';
265 $px3->SetType(array('pie'));
266 $px3->SetTitle($langs->trans("ForObject", $langs->transnoentitiesnoconv("Orders")));
267 $px3->combine = 0.05;
268
269 $px3->draw($filenamenb, $fileurlnb);
270 }
271 }
272 }
273
274
275 if (isModEnabled('invoice') && $user->hasRight('facture', 'lire')) {
276 // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
277 if ($showinvoicenb) {
278 $langs->load("bills");
279 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
280
281 $showpointvalue = 1;
282 $nocolor = 0;
283 $mode = 'customer';
284 $stats_invoice = new FactureStats($this->db, $socid, $mode, ($userid > 0 ? $userid : 0));
285 $data1 = $stats_invoice->getAllByProductEntry($year, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), $max);
286
287 if (empty($data1)) {
288 $showpointvalue = 0;
289 $nocolor = 1;
290 $data1 = array(array(0 => $langs->trans("None"), 1 => 1));
291 }
292 $filenamenb = $dir."/prodserforinvoice-".$year.".png";
293 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&amp;file=prodserforinvoice-'.$year.'.png';
294
295 $px1 = new DolGraph();
296 $mesg = $px1->isGraphKo();
297 if (!$mesg) {
298 $i = 0;
299 $legend = array();
300
301 // Truncate length of legend
302 foreach ($data1 as $key => $val) {
303 $data1[$key][0] = dol_trunc($data1[$key][0], 32);
304 $legend[] = $data1[$key][0];
305 $i++;
306 }
307
308 $px1->SetData($data1);
309 unset($data1);
310
311 if ($nocolor) {
312 $px1->SetDataColor(array(array(220, 220, 220)));
313 }
314 $px1->SetLegend($legend);
315 $px1->setShowLegend(2);
316 if (!empty($conf->dol_optimize_smallscreen)) {
317 $px1->SetWidth(320);
318 }
319 $px1->setShowPointValue($showpointvalue);
320 $px1->setShowPercent(0);
321 $px1->SetMaxValue($px1->GetCeilMaxValue());
322 $px1->SetWidth($WIDTH);
323 $px1->SetHeight($HEIGHT);
324 //$px1->SetYLabel($langs->trans("NumberOfBills"));
325 $px1->SetShading(3);
326 $px1->SetHorizTickIncrement(1);
327 $px1->SetCssPrefix("cssboxes");
328 //$px1->mode='depth';
329 $px1->SetType(array('pie'));
330 $px1->SetTitle($langs->trans("ForObject", $langs->transnoentitiesnoconv("Invoices")));
331 $px1->combine = 0.05;
332
333 $px1->draw($filenamenb, $fileurlnb);
334 }
335 }
336 }
337
338 if (empty($nbofgraph)) {
339 $langs->load("errors");
340 $mesg = $langs->trans("ReadPermissionNotAllowed");
341 }
342 if (empty($conf->use_javascript_ajax)) {
343 $langs->load("errors");
344 $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
345 }
346
347 if (!$mesg) {
348 $stringtoshow = '';
349 $stringtoshow .= '<script nonce="'.getNonce().'" type="text/javascript">
350 jQuery(document).ready(function() {
351 jQuery("#idsubimg'.$this->boxcode.'").click(function() {
352 jQuery("#idfilter'.$this->boxcode.'").toggle();
353 });
354 });
355 </script>';
356 $stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
357 $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
358 $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
359 $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
360 $stringtoshow .= '<input type="hidden" name="page_y" value="">';
361 $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSER_box_'.$this->boxcode.':year,showinvoicenb,showpropalnb,showordernb">';
362 if (isModEnabled("propal") || $user->hasRight('propal', 'lire')) {
363 $stringtoshow .= '<input type="checkbox" name="'.$param_showpropalnb.'"'.($showpropalnb ? ' checked' : '').'> '.$langs->trans("ForProposals");
364 $stringtoshow .= '&nbsp;';
365 }
366 if (isModEnabled('order') || $user->hasRight('commande', 'lire')) {
367 $stringtoshow .= '<input type="checkbox" name="'.$param_showordernb.'"'.($showordernb ? ' checked' : '').'> '.$langs->trans("ForCustomersOrders");
368 }
369 if (isModEnabled('invoice') || $user->hasRight('facture', 'lire')) {
370 $stringtoshow .= '<input type="checkbox" name="'.$param_showinvoicenb.'"'.($showinvoicenb ? ' checked' : '').'> '.$langs->trans("ForCustomersInvoices");
371 $stringtoshow .= ' &nbsp; ';
372 }
373 $stringtoshow .= '<br>';
374 $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$year.'">';
375 $stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto('', 'refresh.png', '', 0, 1).'">';
376 $stringtoshow .= '</form>';
377 $stringtoshow .= '</div>';
378
379 if ($nbofgraph == 1) {
380 if ($showpropalnb && $px2 !== null) {
381 $stringtoshow .= $px2->show();
382 } elseif ($showordernb && $px3 !== null) {
383 $stringtoshow .= $px3->show();
384 } elseif ($px1 !== null) {
385 $stringtoshow .= $px1->show();
386 }
387 }
388 if ($nbofgraph == 2) {
389 $stringtoshow .= '<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
390 if (isModEnabled('propal') && $showpropalnb && $px2 !== null) {
391 $stringtoshow .= $px2->show();
392 } elseif (isModEnabled('order') && $showordernb && $px3 !== null) {
393 $stringtoshow .= $px3->show();
394 }
395 $stringtoshow .= '</div><div class="fichehalfright">';
396 if (isModEnabled('invoice') && $showinvoicenb && $px1 !== null) {
397 $stringtoshow .= $px1->show();
398 } elseif (isModEnabled('order') && $showordernb && $px3 !== null) {
399 $stringtoshow .= $px3->show();
400 }
401 $stringtoshow .= '</div></div></div>';
402 }
403 if ($nbofgraph == 3 && $px1 !== null && $px2 !== null && $px3 !== null) {
404 $stringtoshow .= '<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
405 $stringtoshow .= $px2->show();
406 $stringtoshow .= '</div><div class="fichehalfright">';
407 $stringtoshow .= $px3->show();
408 $stringtoshow .= '</div></div></div>';
409 $stringtoshow .= '<div class="fichecenter"><div class="containercenter">';
410 $stringtoshow .= $px1->show();
411 $stringtoshow .= '</div></div>';
412 }
413 $this->info_box_contents[0][0] = array(
414 'tr' => 'class="oddeven nohover"',
415 'td' => 'class="nohover center"',
416 'textnoformat' => $stringtoshow,
417 );
418 } else {
419 $this->info_box_contents[0][0] = array(
420 'td' => 'class="nohover left"',
421 'maxlength' => 500,
422 'text' => '<span class="opacitymedium">'.$mesg.'</span>'
423 );
424 }
425 }
426
427
428
437 public function showBox($head = null, $contents = null, $nooutput = 0)
438 {
439 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
440 }
441}
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