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_orders_permonth.php
20 *	\ingroup    commandes
21 *	\brief      Box to show graph of orders 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 orders
28 */
29class box_graph_orders_permonth extends ModeleBoxes
30{
31	public $boxcode = "orderspermonth";
32	public $boximg = "object_order";
33	public $boxlabel = "BoxCustomersOrdersPerMonth";
34	public $depends = array("commande");
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->commande->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.'/commande/class/commande.class.php';
75		//$commandestatic=new Commande($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("BoxCustomersOrdersPerMonth", $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->commande->lire)
99		{
100			$langs->load("orders");
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.'/commande/class/commandestats.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 CommandeStats($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."ordersnbinyear-".$endyear.".png";
137				// default value for customer mode
138				$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&amp;file=ordersnbinyear-'.$endyear.'.png';
139				if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&amp;file=ordersnbinyear-'.$endyear.'.png';
140
141				$px1 = new DolGraph();
142				$mesg = $px1->isGraphKo();
143				if (!$mesg)
144				{
145					$px1->SetData($data1);
146					unset($data1);
147					$i = $startyear;
148					$legend = array();
149					while ($i <= $endyear)
150					{
151						if ($startmonth != 1)
152						{
153							$legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
154						} else {
155							$legend[] = $i;
156						}
157						$i++;
158					}
159					$px1->SetLegend($legend);
160					$px1->SetMaxValue($px1->GetCeilMaxValue());
161					$px1->SetWidth($WIDTH);
162					$px1->SetHeight($HEIGHT);
163					$px1->SetYLabel($langs->trans("NumberOfOrders"));
164					$px1->SetShading(3);
165					$px1->SetHorizTickIncrement(1);
166					$px1->SetCssPrefix("cssboxes");
167					$px1->mode = 'depth';
168					$px1->SetTitle($langs->trans("NumberOfOrdersByMonth"));
169
170					$px1->draw($filenamenb, $fileurlnb);
171				}
172			}
173
174			// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
175			if ($showtot)
176			{
177				$data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
178
179				$filenamenb = $dir."/".$prefix."ordersamountinyear-".$endyear.".png";
180				// default value for customer mode
181				$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&amp;file=ordersamountinyear-'.$endyear.'.png';
182				if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&amp;file=ordersamountinyear-'.$endyear.'.png';
183
184				$px2 = new DolGraph();
185				$mesg = $px2->isGraphKo();
186				if (!$mesg)
187				{
188					$px2->SetData($data2);
189					unset($data2);
190					$i = $startyear; $legend = array();
191					while ($i <= $endyear)
192					{
193						if ($startmonth != 1)
194						{
195							$legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
196						} else {
197							$legend[] = $i;
198						}
199						$i++;
200					}
201					$px2->SetLegend($legend);
202					$px2->SetMaxValue($px2->GetCeilMaxValue());
203					$px2->SetWidth($WIDTH);
204					$px2->SetHeight($HEIGHT);
205					$px2->SetYLabel($langs->trans("AmountOfOrdersHT"));
206					$px2->SetShading(3);
207					$px2->SetHorizTickIncrement(1);
208					$px2->SetCssPrefix("cssboxes");
209					$px2->mode = 'depth';
210					$px2->SetTitle($langs->trans("AmountOfOrdersByMonthHT"));
211
212					$px2->draw($filenamenb, $fileurlnb);
213				}
214			}
215
216			if (empty($conf->use_javascript_ajax))
217			{
218				$langs->load("errors");
219				$mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
220			}
221
222			if (!$mesg)
223			{
224				$stringtoshow = '';
225				$stringtoshow .= '<script type="text/javascript" language="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="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
238				$stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfOrdersByMonth");
239				$stringtoshow .= ' &nbsp; ';
240				$stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfOrdersByMonthHT");
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', '', '', 1).'">';
244				$stringtoshow .= '</form>';
245				$stringtoshow .= '</div>';
246				if ($shownb && $showtot)
247				{
248					$stringtoshow .= '<div class="fichecenter">';
249					$stringtoshow .= '<div class="fichehalfleft">';
250				}
251				if ($shownb) $stringtoshow .= $px1->show();
252				if ($shownb && $showtot)
253				{
254					$stringtoshow .= '</div>';
255					$stringtoshow .= '<div class="fichehalfright">';
256				}
257				if ($showtot) $stringtoshow .= $px2->show();
258				if ($shownb && $showtot)
259				{
260					$stringtoshow .= '</div>';
261					$stringtoshow .= '</div>';
262				}
263				$this->info_box_contents[0][0] = array(
264					'tr'=>'class="oddeven nohover"',
265					'td' => 'class="nohover center"',
266					'textnoformat'=>$stringtoshow,
267				);
268			} else {
269				$this->info_box_contents[0][0] = array(
270					'tr'=>'class="oddeven nohover"',
271					'td' => 'class="nohover left"',
272					'maxlength'=>500,
273					'text' => $mesg,
274				);
275			}
276		} else {
277			$this->info_box_contents[0][0] = array(
278				'td' => 'class="nohover opacitymedium left"',
279				'text' => $langs->trans("ReadPermissionNotAllowed")
280			);
281		}
282	}
283
284	/**
285	 *  Method to show box
286	 *
287	 *  @param	array	$head       Array with properties of box title
288	 *  @param  array	$contents   Array with properties of box lines
289	 *  @param	int		$nooutput	No print, only return string
290	 *  @return	string
291	 */
292	public function showBox($head = null, $contents = null, $nooutput = 0)
293	{
294		return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
295	}
296}
297