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