dolibarr 21.0.0-alpha
box_graph_invoices_permonth.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.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
24include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
25
26
31{
32 public $boxcode = "invoicespermonth";
33 public $boximg = "object_bill";
34 public $boxlabel = "BoxCustomersInvoicesPerMonth";
35 public $depends = array("facture");
36
37 public $widgettype = 'graph';
38
39
46 public function __construct($db, $param)
47 {
48 global $user;
49
50 $this->db = $db;
51
52 $this->hidden = !$user->hasRight('facture', 'lire');
53 }
54
61 public function loadBox($max = 5)
62 {
63 global $conf, $user, $langs;
64
65 $this->max = $max;
66
67 $refreshaction = 'refresh_'.$this->boxcode;
68
69 //include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
70 //$facturestatic=new Facture($this->db);
71
72 $startmonth = getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
73 if (!getDolGlobalString('GRAPH_USE_FISCAL_YEAR')) {
74 $startmonth = 1;
75 }
76
77 $text = $langs->trans("BoxCustomersInvoicesPerMonth", $max);
78 $this->info_box_head = array(
79 'text' => $text,
80 'limit'=> dol_strlen($text),
81 'graph'=> 1,
82 'sublink'=>'',
83 'subtext'=>$langs->trans("Filter"),
84 'subpicto'=>'filter.png',
85 'subclass'=>'linkobject boxfilter',
86 'target'=>'none' // Set '' to get target="_blank"
87 );
88
89 $dir = ''; // We don't need a path because image file will not be saved into disk
90 $prefix = '';
91 $socid = 0;
92 if ($user->socid) {
93 $socid = $user->socid;
94 }
95 if (!$user->hasRight('societe', 'client', 'voir')) {
96 $prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
97 }
98
99 if ($user->hasRight('facture', 'lire')) {
100 $mesg = '';
101
102 $param_year = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
103 $param_shownb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_shownb';
104 $param_showtot = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showtot';
105
106 include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
107 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
108 $autosetarray = preg_split("/[,;:]+/", GETPOST('DOL_AUTOSET_COOKIE'));
109 if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode, $autosetarray)) {
110 $endyear = GETPOSTINT($param_year);
111 $shownb = GETPOST($param_shownb, 'alpha');
112 $showtot = GETPOST($param_showtot, 'alpha');
113 } else {
114 $tmparray = (!empty($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode]) ? json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode], true) : array());
115 $endyear = (!empty($tmparray['year']) ? $tmparray['year'] : '');
116 $shownb = (!empty($tmparray['shownb']) ? $tmparray['shownb'] : '');
117 $showtot = (!empty($tmparray['showtot']) ? $tmparray['showtot'] : '');
118 }
119 if (empty($shownb) && empty($showtot)) {
120 $shownb = 1;
121 $showtot = 1;
122 }
123 $nowarray = dol_getdate(dol_now(), true);
124 if (empty($endyear)) {
125 $endyear = $nowarray['year'];
126 }
127 $startyear = $endyear - getDolGlobalInt('MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH', 3) + 1; // Default is 3 years
128
129 $mode = 'customer';
130 $WIDTH = (($shownb && $showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
131 $HEIGHT = '192';
132
133 $stats = new FactureStats($this->db, $socid, $mode, 0);
134 $stats->where = "f.fk_statut > 0";
135
136 // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
137 if ($shownb) {
138 $data1 = $stats->getNbByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
139
140 $filenamenb = $dir."/".$prefix."invoicesnbinyear-".$endyear.".png";
141 // default value for customer mode
142 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesnbinyear-'.$endyear.'.png';
143
144 $px1 = new DolGraph();
145 $mesg = $px1->isGraphKo();
146 if (!$mesg) {
147 $langs->load("bills");
148
149 $px1->SetData($data1);
150 unset($data1);
151 $i = $startyear;
152 $legend = array();
153 while ($i <= $endyear) {
154 if ($startmonth != 1) {
155 $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
156 } else {
157 $legend[] = $i;
158 }
159 $i++;
160 }
161
162 $px1->SetLegend($legend);
163 $px1->SetMaxValue($px1->GetCeilMaxValue());
164 $px1->SetWidth($WIDTH);
165 $px1->SetHeight($HEIGHT);
166 $px1->SetYLabel($langs->trans("NumberOfBills"));
167 $px1->SetShading(3);
168 $px1->SetHorizTickIncrement(1);
169 $px1->SetCssPrefix("cssboxes");
170 $px1->mode = 'depth';
171 $px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
172
173 $px1->draw($filenamenb, $fileurlnb);
174 }
175 }
176
177 // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
178 if ($showtot) {
179 $data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
180
181 $filenamenb = $dir."/".$prefix."invoicesamountinyear-".$endyear.".png";
182 // default value for customer mode
183 $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesamountinyear-'.$endyear.'.png';
184
185 $px2 = new DolGraph();
186 $mesg = $px2->isGraphKo();
187 if (!$mesg) {
188 $langs->load("bills");
189
190 $px2->SetData($data2);
191 unset($data2);
192 $i = $startyear;
193 $legend = array();
194 while ($i <= $endyear) {
195 if ($startmonth != 1) {
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 $langs->load("errors");
219 $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
220 }
221
222 if (!$mesg) {
223 $stringtoshow = '';
224 $stringtoshow .= '<script nonce="'.getNonce().'" type="text/javascript">
225 jQuery(document).ready(function() {
226 jQuery("#idsubimg'.$this->boxcode.'").click(function() {
227 jQuery("#idfilter'.$this->boxcode.'").toggle();
228 });
229 });
230 </script>';
231 $stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
232 $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
233 $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
234 $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
235 $stringtoshow .= '<input type="hidden" name="page_y" value="">';
236 $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
237 $stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfBillsByMonth");
238 $stringtoshow .= ' &nbsp; ';
239 $stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfBillsByMonthHT");
240 $stringtoshow .= '<br>';
241 $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
242 $stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
243 $stringtoshow .= '</form>';
244 $stringtoshow .= '</div>';
245 if ($shownb && $showtot) {
246 $stringtoshow .= '<div class="fichecenter">';
247 $stringtoshow .= '<div class="fichehalfleft">';
248 }
249 if ($shownb) {
250 $stringtoshow .= $px1->show();
251 }
252 if ($shownb && $showtot) {
253 $stringtoshow .= '</div>';
254 $stringtoshow .= '<div class="fichehalfright">';
255 }
256 if ($showtot) {
257 $stringtoshow .= $px2->show();
258 }
259 if ($shownb && $showtot) {
260 $stringtoshow .= '</div>';
261 $stringtoshow .= '</div>';
262 }
263 $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover center"', 'textnoformat'=>$stringtoshow);
264 } else {
265 $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover left"', 'maxlength'=>500, 'text' => $mesg);
266 }
267 } else {
268 $this->info_box_contents[0][0] = array(
269 'td' => 'class="nohover left"',
270 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
271 );
272 }
273 }
274
283 public function showBox($head = null, $contents = null, $nooutput = 0)
284 {
285 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
286 }
287}
Class to build graphs.
Class to manage stats for invoices (customer and supplier)
Class ModeleBoxes.
Class to manage the box to show invoices per month graph.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.