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
18/**
19 *	\file       htdocs/core/boxes/box_graph_invoices_permonth.php
20 *	\ingroup    factures
21 *	\brief      Box to show graph of invoices per month
22 */
23include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
24
25
26/**
27 * Class to manage the box to show last invoices
28 */
29class box_graph_invoices_permonth extends ModeleBoxes
30{
31	public $boxcode = "invoicespermonth";
32	public $boximg = "object_bill";
33	public $boxlabel = "BoxCustomersInvoicesPerMonth";
34	public $depends = array("facture");
35
36	/**
37	 * @var DoliDB Database handler.
38	 */
39	public $db;
40
41	public $info_box_head = array();
42	public $info_box_contents = array();
43
44	public $widgettype = 'graph';
45
46
47	/**
48	 *  Constructor
49	 *
50	 * 	@param	DoliDB	$db			Database handler
51	 *  @param	string	$param		More parameters
52	 */
53	public function __construct($db, $param)
54	{
55		global $user;
56
57		$this->db = $db;
58
59		$this->hidden = !($user->rights->facture->lire);
60	}
61
62	/**
63	 *  Load data into info_box_contents array to show array later.
64	 *
65	 *  @param	int		$max        Maximum number of records to load
66	 *  @return	void
67	 */
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.'/compta/facture/class/facture.class.php';
77		//$facturestatic=new Facture($this->db);
78
79		$startmonth = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1;
80		if (empty($conf->global->GRAPH_USE_FISCAL_YEAR)) {
81			$startmonth = 1;
82		}
83
84		$text = $langs->trans("BoxCustomersInvoicesPerMonth", $max);
85		$this->info_box_head = array(
86			'text' => $text,
87			'limit'=> dol_strlen($text),
88			'graph'=> 1,
89			'sublink'=>'',
90			'subtext'=>$langs->trans("Filter"),
91			'subpicto'=>'filter.png',
92			'subclass'=>'linkobject boxfilter',
93			'target'=>'none'	// Set '' to get target="_blank"
94		);
95
96		$dir = ''; // We don't need a path because image file will not be saved into disk
97		$prefix = '';
98		$socid = 0;
99		if ($user->socid) {
100			$socid = $user->socid;
101		}
102		if (!$user->rights->societe->client->voir || $socid) {
103			$prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
104		}
105
106		if ($user->rights->facture->lire) {
107			$mesg = '';
108
109			$param_year = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
110			$param_shownb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_shownb';
111			$param_showtot = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showtot';
112
113			include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
114			include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
115			$autosetarray = preg_split("/[,;:]+/", GETPOST('DOL_AUTOSET_COOKIE'));
116			if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode, $autosetarray)) {
117				$endyear = GETPOST($param_year, 'int');
118				$shownb = GETPOST($param_shownb, 'alpha');
119				$showtot = GETPOST($param_showtot, 'alpha');
120			} else {
121				$tmparray = (!empty($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode]) ? json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode], true) : array());
122				$endyear = (!empty($tmparray['year']) ? $tmparray['year'] : '');
123				$shownb = (!empty($tmparray['shownb']) ? $tmparray['shownb'] : '');
124				$showtot = (!empty($tmparray['showtot']) ? $tmparray['showtot'] : '');
125			}
126			if (empty($shownb) && empty($showtot)) {
127				$shownb = 1;
128				$showtot = 1;
129			}
130			$nowarray = dol_getdate(dol_now(), true);
131			if (empty($endyear)) {
132				$endyear = $nowarray['year'];
133			}
134			$startyear = $endyear - (empty($conf->global->MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH) ? 2 : ($conf->global->MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH - 1));
135
136			$mode = 'customer';
137			$WIDTH = (($shownb && $showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
138			$HEIGHT = '192';
139
140			$stats = new FactureStats($this->db, $socid, $mode, 0);
141
142			// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
143			if ($shownb) {
144				$data1 = $stats->getNbByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
145
146				$filenamenb = $dir."/".$prefix."invoicesnbinyear-".$endyear.".png";
147				// default value for customer mode
148				$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesnbinyear-'.$endyear.'.png';
149				if ($mode == 'supplier') {
150					$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicessuppliernbinyear-'.$endyear.'.png';
151				}
152
153				$px1 = new DolGraph();
154				$mesg = $px1->isGraphKo();
155				if (!$mesg) {
156					$langs->load("bills");
157
158					$px1->SetData($data1);
159					unset($data1);
160					$i = $startyear;
161					$legend = array();
162					while ($i <= $endyear) {
163						if ($startmonth != 1) {
164							$legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
165						} else {
166							$legend[] = $i;
167						}
168						$i++;
169					}
170
171					$px1->SetLegend($legend);
172					$px1->SetMaxValue($px1->GetCeilMaxValue());
173					$px1->SetWidth($WIDTH);
174					$px1->SetHeight($HEIGHT);
175					$px1->SetYLabel($langs->trans("NumberOfBills"));
176					$px1->SetShading(3);
177					$px1->SetHorizTickIncrement(1);
178					$px1->SetCssPrefix("cssboxes");
179					$px1->mode = 'depth';
180					$px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
181
182					$px1->draw($filenamenb, $fileurlnb);
183				}
184			}
185
186			// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
187			if ($showtot) {
188				$data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
189
190				$filenamenb = $dir."/".$prefix."invoicesamountinyear-".$endyear.".png";
191				// default value for customer mode
192				$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesamountinyear-'.$endyear.'.png';
193				if ($mode == 'supplier') {
194					$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicessupplieramountinyear-'.$endyear.'.png';
195				}
196
197				$px2 = new DolGraph();
198				$mesg = $px2->isGraphKo();
199				if (!$mesg) {
200					$langs->load("bills");
201
202					$px2->SetData($data2);
203					unset($data2);
204					$i = $startyear;
205					$legend = array();
206					while ($i <= $endyear) {
207						if ($startmonth != 1) {
208							$legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
209						} else {
210							$legend[] = $i;
211						}
212						$i++;
213					}
214					$px2->SetLegend($legend);
215					$px2->SetMaxValue($px2->GetCeilMaxValue());
216					$px2->SetWidth($WIDTH);
217					$px2->SetHeight($HEIGHT);
218					$px2->SetYLabel($langs->trans("AmountOfBillsHT"));
219					$px2->SetShading(3);
220					$px2->SetHorizTickIncrement(1);
221					$px2->SetCssPrefix("cssboxes");
222					$px2->mode = 'depth';
223					$px2->SetTitle($langs->trans("AmountOfBillsByMonthHT"));
224
225					$px2->draw($filenamenb, $fileurlnb);
226				}
227			}
228
229			if (empty($conf->use_javascript_ajax)) {
230				$langs->load("errors");
231				$mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
232			}
233
234			if (!$mesg) {
235				$stringtoshow = '';
236				$stringtoshow .= '<script type="text/javascript" language="javascript">
237					jQuery(document).ready(function() {
238						jQuery("#idsubimg'.$this->boxcode.'").click(function() {
239							jQuery("#idfilter'.$this->boxcode.'").toggle();
240						});
241					});
242					</script>';
243				$stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
244				$stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
245				$stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
246				$stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
247				$stringtoshow .= '<input type="hidden" name="page_y" value="">';
248				$stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
249				$stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfBillsByMonth");
250				$stringtoshow .= ' &nbsp; ';
251				$stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfBillsByMonthHT");
252				$stringtoshow .= '<br>';
253				$stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
254				$stringtoshow .= '<input class="reposition inline-block valigntextbottom" type="image" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
255				$stringtoshow .= '</form>';
256				$stringtoshow .= '</div>';
257				if ($shownb && $showtot) {
258					$stringtoshow .= '<div class="fichecenter">';
259					$stringtoshow .= '<div class="fichehalfleft">';
260				}
261				if ($shownb) {
262					$stringtoshow .= $px1->show();
263				}
264				if ($shownb && $showtot) {
265					$stringtoshow .= '</div>';
266					$stringtoshow .= '<div class="fichehalfright">';
267				}
268				if ($showtot) {
269					$stringtoshow .= $px2->show();
270				}
271				if ($shownb && $showtot) {
272					$stringtoshow .= '</div>';
273					$stringtoshow .= '</div>';
274				}
275				$this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover center"', 'textnoformat'=>$stringtoshow);
276			} else {
277				$this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover left"', 'maxlength'=>500, 'text' => $mesg);
278			}
279		} else {
280			$this->info_box_contents[0][0] = array(
281				'td' => 'class="nohover left"',
282				'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
283			);
284		}
285	}
286
287	/**
288	 *	Method to show box
289	 *
290	 *	@param	array	$head       Array with properties of box title
291	 *	@param  array	$contents   Array with properties of box lines
292	 *  @param	int		$nooutput	No print, only return string
293	 *	@return	string
294	 */
295	public function showBox($head = null, $contents = null, $nooutput = 0)
296	{
297		return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
298	}
299}
300