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