dolibarr  16.0.5
box_graph_invoices_supplier_permonth.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013 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 <https://www.gnu.org/licenses/>.
16  */
17 
23 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
24 
25 
30 {
31  public $boxcode = "invoicessupplierpermonth";
32  public $boximg = "object_bill";
33  public $boxlabel = "BoxSuppliersInvoicesPerMonth";
34  public $depends = array("fournisseur");
35 
39  public $db;
40 
41  public $info_box_head = array();
42  public $info_box_contents = array();
43 
44  public $widgettype = 'graph';
45 
46 
53  public function __construct($db, $param)
54  {
55  global $user;
56 
57  $this->db = $db;
58 
59  $this->hidden = empty($user->rights->fournisseur->facture->lire);
60  }
61 
68  public function loadBox($max = 5)
69  {
70  global $conf, $user, $langs;
71 
72  $this->max = $max;
73 
74  $refreshaction = 'refresh_'.$this->boxcode;
75 
76  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
77 
78  $startmonth = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1;
79  if (empty($conf->global->GRAPH_USE_FISCAL_YEAR)) {
80  $startmonth = 1;
81  }
82 
83  $text = $langs->trans("BoxSuppliersInvoicesPerMonth", $max);
84  $this->info_box_head = array(
85  'text' => $text,
86  'limit'=> dol_strlen($text),
87  'graph'=> 1,
88  'sublink'=>'',
89  'subtext'=>$langs->trans("Filter"),
90  'subpicto'=>'filter.png',
91  'subclass'=>'linkobject boxfilter',
92  'target'=>'none' // Set '' to get target="_blank"
93  );
94 
95  $dir = ''; // We don't need a path because image file will not be saved into disk
96  $prefix = '';
97  $socid = 0;
98  if ($user->socid) {
99  $socid = $user->socid;
100  }
101  if (empty($user->rights->societe->client->voir) || $socid) {
102  $prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
103  }
104 
105  if ($user->rights->fournisseur->facture->lire) {
106  $param_year = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
107  $param_shownb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_shownb';
108  $param_showtot = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showtot';
109 
110  include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
111  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
112  $autosetarray = preg_split("/[,;:]+/", GETPOST('DOL_AUTOSET_COOKIE'));
113  if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode, $autosetarray)) {
114  $endyear = GETPOST($param_year, 'int');
115  $shownb = GETPOST($param_shownb, 'alpha');
116  $showtot = GETPOST($param_showtot, 'alpha');
117  } else {
118  $tmparray = (!empty($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode]) ? json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode], true) : array());
119  $endyear = (!empty($tmparray['year']) ? $tmparray['year'] : '');
120  $shownb = (!empty($tmparray['shownb']) ? $tmparray['shownb'] : '');
121  $showtot = (!empty($tmparray['showtot']) ? $tmparray['showtot'] : '');
122  }
123  if (empty($shownb) && empty($showtot)) {
124  $shownb = 1;
125  $showtot = 1;
126  }
127  $nowarray = dol_getdate(dol_now(), true);
128  if (empty($endyear)) {
129  $endyear = $nowarray['year'];
130  }
131  $startyear = $endyear - (empty($conf->global->MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH) ? 2 : ($conf->global->MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH - 1));
132 
133  $mode = 'supplier';
134  $WIDTH = (($shownb && $showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
135  $HEIGHT = '192';
136 
137  $stats = new FactureStats($this->db, $socid, $mode, 0);
138 
139  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
140  if ($shownb) {
141  $data1 = $stats->getNbByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
142 
143  $filenamenb = $dir."/".$prefix."invoicessuppliernbinyear-".$endyear.".png";
144  // default value for customer mode
145  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicessuppliernbinyear-'.$endyear.'.png';
146 
147  $px1 = new DolGraph();
148  $mesg = $px1->isGraphKo();
149  if (!$mesg) {
150  $langs->load("bills");
151 
152  $px1->SetData($data1);
153  unset($data1);
154  $i = $startyear;
155  $legend = array();
156  while ($i <= $endyear) {
157  if ($startmonth != 1) {
158  $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
159  } else {
160  $legend[] = $i;
161  }
162  $i++;
163  }
164  $px1->SetLegend($legend);
165  $px1->SetMaxValue($px1->GetCeilMaxValue());
166  $px1->SetWidth($WIDTH);
167  $px1->SetHeight($HEIGHT);
168  $px1->SetYLabel($langs->trans("NumberOfBills"));
169  $px1->SetShading(3);
170  $px1->SetHorizTickIncrement(1);
171  $px1->SetCssPrefix("cssboxes");
172  $px1->mode = 'depth';
173  $px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
174 
175  $px1->draw($filenamenb, $fileurlnb);
176  }
177  }
178 
179  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
180  if ($showtot) {
181  $data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
182 
183  $filenamenb = $dir."/".$prefix."invoicessupplieramountinyear-".$endyear.".png";
184  // default value for customer mode
185  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicessupplieramountinyear-'.$endyear.'.png';
186 
187  $px2 = new DolGraph();
188  $mesg = $px2->isGraphKo();
189  if (!$mesg) {
190  $langs->load("bills");
191 
192  $px2->SetData($data2);
193  unset($data2);
194  $i = $startyear;
195  $legend = array();
196  while ($i <= $endyear) {
197  if ($startmonth != 1) {
198  $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
199  } else {
200  $legend[] = $i;
201  }
202  $i++;
203  }
204  $px2->SetLegend($legend);
205  $px2->SetMaxValue($px2->GetCeilMaxValue());
206  $px2->SetWidth($WIDTH);
207  $px2->SetHeight($HEIGHT);
208  $px2->SetYLabel($langs->trans("AmountOfBillsHT"));
209  $px2->SetShading(3);
210  $px2->SetHorizTickIncrement(1);
211  $px2->SetCssPrefix("cssboxes");
212  $px2->mode = 'depth';
213  $px2->SetTitle($langs->trans("AmountOfBillsByMonthHT"));
214 
215  $px2->draw($filenamenb, $fileurlnb);
216  }
217  }
218 
219  if (empty($conf->use_javascript_ajax)) {
220  $langs->load("errors");
221  $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
222  }
223 
224  if (!$mesg) {
225  $stringtoshow = '';
226  $stringtoshow .= '<script type="text/javascript">
227  jQuery(document).ready(function() {
228  jQuery("#idsubimg'.$this->boxcode.'").click(function() {
229  jQuery("#idfilter'.$this->boxcode.'").toggle();
230  });
231  });
232  </script>';
233  $stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
234  $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
235  $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
236  $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
237  $stringtoshow .= '<input type="hidden" name="page_y" value="">';
238  $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
239  $stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfBillsByMonth");
240  $stringtoshow .= ' &nbsp; ';
241  $stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfBillsByMonthHT");
242  $stringtoshow .= '<br>';
243  $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
244  $stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
245  $stringtoshow .= '</form>';
246  $stringtoshow .= '</div>';
247  if ($shownb && $showtot) {
248  $stringtoshow .= '<div class="fichecenter">';
249  $stringtoshow .= '<div class="fichehalfleft">';
250  }
251  if ($shownb) {
252  $stringtoshow .= $px1->show();
253  }
254  if ($shownb && $showtot) {
255  $stringtoshow .= '</div>';
256  $stringtoshow .= '<div class="fichehalfright">';
257  }
258  if ($showtot) {
259  $stringtoshow .= $px2->show();
260  }
261  if ($shownb && $showtot) {
262  $stringtoshow .= '</div>';
263  $stringtoshow .= '</div>';
264  }
265  $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover center"', 'textnoformat'=>$stringtoshow);
266  } else {
267  $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover left"', 'maxlength'=>500, 'text' => $mesg);
268  }
269  } else {
270  $this->info_box_contents[0][0] = array(
271  'td' => 'class="nohover left"',
272  'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
273  );
274  }
275  }
276 
285  public function showBox($head = null, $contents = null, $nooutput = 0)
286  {
287  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
288  }
289 }
dol_getdate
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
Definition: functions.lib.php:2713
db
$conf db
API class for accounts.
Definition: inc.php:41
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
DolGraph
Class to build graphs.
Definition: dolgraph.class.php:40
box_graph_invoices_supplier_permonth\__construct
__construct($db, $param)
Constructor.
Definition: box_graph_invoices_supplier_permonth.php:53
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
FactureStats
Class to manage stats for invoices (customer and supplier)
Definition: facturestats.class.php:34
box_graph_invoices_supplier_permonth\loadBox
loadBox($max=5)
Load data into info_box_contents array to show array later.
Definition: box_graph_invoices_supplier_permonth.php:68
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
box_graph_invoices_supplier_permonth
Class to manage the box to show last invoices.
Definition: box_graph_invoices_supplier_permonth.php:29
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
box_graph_invoices_supplier_permonth\showBox
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Definition: box_graph_invoices_supplier_permonth.php:285
ModeleBoxes
Class ModeleBoxes.
Definition: modules_boxes.php:34