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