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_propales_permonth.php
20 *	\ingroup    propales
21 *	\brief      Box to show graph of proposals 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 propals
28 */
29class box_graph_propales_permonth extends ModeleBoxes
30{
31	public $boxcode = "propalpermonth";
32	public $boximg = "object_propal";
33	public $boxlabel = "BoxProposalsPerMonth";
34	public $depends = array("propal");
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->propale->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.'/comm/propal/class/propal.class.php';
77		//$propalstatic=new Propal($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		$langs->load("propal");
85
86		$text = $langs->trans("BoxProposalsPerMonth", $max);
87		$this->info_box_head = array(
88				'text' => $text,
89				'limit'=> dol_strlen($text),
90				'graph'=> 1, // Set to 1 if it's a box graph
91				'sublink'=>'',
92				'subtext'=>$langs->trans("Filter"),
93				'subpicto'=>'filter.png',
94				'subclass'=>'linkobject boxfilter',
95				'target'=>'none'	// Set '' to get target="_blank"
96		);
97
98		$dir = ''; // We don't need a path because image file will not be saved into disk
99		$prefix = '';
100		$socid = 0;
101		if ($user->socid) {
102			$socid = $user->socid;
103		}
104		if (!$user->rights->societe->client->voir || $socid) {
105			$prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
106		}
107
108		if ($user->rights->propale->lire) {
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.'/comm/propal/class/propalestats.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			$WIDTH = (($shownb && $showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
137			$HEIGHT = '192';
138
139			$stats = new PropaleStats($this->db, $socid, 0);
140
141			// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
142			if ($shownb) {
143				$data1 = $stats->getNbByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
144				$datatype1 = array_pad(array(), ($endyear - $startyear + 1), 'bars');
145
146				$filenamenb = $dir."/".$prefix."propalsnbinyear-".$endyear.".png";
147				$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&amp;file=propalsnbinyear-'.$endyear.'.png';
148
149				$px1 = new DolGraph();
150				$mesg = $px1->isGraphKo();
151				if (!$mesg) {
152					$px1->SetType($datatype1);
153					$px1->SetData($data1);
154					unset($data1);
155					$i = $startyear;
156					$legend = array();
157					while ($i <= $endyear) {
158						if ($startmonth != 1) {
159							$legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
160						} else {
161							$legend[] = $i;
162						}
163						$i++;
164					}
165					$px1->SetLegend($legend);
166					$px1->SetMaxValue($px1->GetCeilMaxValue());
167					$px1->SetWidth($WIDTH);
168					$px1->SetHeight($HEIGHT);
169					$px1->SetYLabel($langs->trans("NumberOfProposals"));
170					$px1->SetShading(3);
171					$px1->SetHorizTickIncrement(1);
172					$px1->SetCssPrefix("cssboxes");
173					$px1->mode = 'depth';
174					$px1->SetTitle($langs->trans("NumberOfProposalsByMonth"));
175
176					$px1->draw($filenamenb, $fileurlnb);
177				}
178			}
179
180			// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
181			if ($showtot) {
182				$data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
183				$datatype2 = array_pad(array(), ($endyear - $startyear + 1), 'bars');
184				//$datatype2 = array('lines','bars');
185
186				$filenamenb = $dir."/".$prefix."propalsamountinyear-".$endyear.".png";
187				if (!empty($mode)) {
188					if ($mode == 'customer') {
189						$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&amp;file=propalsamountinyear-'.$endyear.'.png';
190					}
191					if ($mode == 'supplier') {
192						$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstatssupplier&amp;file=propalsamountinyear-'.$endyear.'.png';
193					}
194				}
195
196				$px2 = new DolGraph();
197				$mesg = $px2->isGraphKo();
198				if (!$mesg) {
199					$px2->SetType($datatype2);
200					$px2->SetData($data2);
201					unset($data2);
202					$i = $startyear;
203					$legend = array();
204					while ($i <= $endyear) {
205						if ($startmonth != 1) {
206							$legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
207						} else {
208							$legend[] = $i;
209						}
210						$i++;
211					}
212					$px2->SetLegend($legend);
213					$px2->SetMaxValue($px2->GetCeilMaxValue());
214					$px2->SetWidth($WIDTH);
215					$px2->SetHeight($HEIGHT);
216					$px2->SetYLabel($langs->trans("AmountOfProposalsHT"));
217					$px2->SetShading(3);
218					$px2->SetHorizTickIncrement(1);
219					$px2->SetCssPrefix("cssboxes");
220					$px2->mode = 'depth';
221					$px2->SetTitle($langs->trans("AmountOfProposalsByMonthHT"));
222
223					$px2->draw($filenamenb, $fileurlnb);
224				}
225			}
226
227			if (empty($conf->use_javascript_ajax)) {
228				$langs->load("errors");
229				$mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
230			}
231
232			if (!$mesg) {
233				$stringtoshow = '';
234				$stringtoshow .= '<script type="text/javascript" language="javascript">
235					jQuery(document).ready(function() {
236						jQuery("#idsubimg'.$this->boxcode.'").click(function() {
237							jQuery("#idfilter'.$this->boxcode.'").toggle();
238						});
239					});
240					</script>';
241				$stringtoshow .= '<div class="center hideobject divboxfilter" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
242				$stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
243				$stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
244				$stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
245				$stringtoshow .= '<input type="hidden" name="page_y" value="">';
246				$stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
247				$stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfProposalsByMonth");
248				$stringtoshow .= ' &nbsp; ';
249				$stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfProposalsByMonthHT");
250				$stringtoshow .= '<br>';
251				$stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
252				$stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
253				$stringtoshow .= '</form>';
254				$stringtoshow .= '</div>';
255				if ($shownb && $showtot) {
256					$stringtoshow .= '<div class="fichecenter">';
257					$stringtoshow .= '<div class="fichehalfleft">';
258				}
259				if ($shownb) {
260					$stringtoshow .= $px1->show();
261				}
262				if ($shownb && $showtot) {
263					$stringtoshow .= '</div>';
264					$stringtoshow .= '<div class="fichehalfright">';
265				}
266				if ($showtot) {
267					$stringtoshow .= $px2->show();
268				}
269				if ($shownb && $showtot) {
270					$stringtoshow .= '</div>';
271					$stringtoshow .= '</div>';
272				}
273				$this->info_box_contents[0][0] = array(
274					'tr'=>'class="oddeven nohover"',
275					'td' => 'class="nohover center"',
276					'textnoformat'=>$stringtoshow,
277				);
278			} else {
279				$this->info_box_contents[0][0] = array(
280					'tr'=>'class="oddeven nohover"',
281					'td' => 'class="nohover left"',
282					'maxlength' => 500,
283					'text' => $mesg,
284				);
285			}
286		} else {
287			$this->info_box_contents[0][0] = array(
288				'td' => 'class="nohover opacitymedium left"',
289				'text' => $langs->trans("ReadPermissionNotAllowed")
290			);
291		}
292	}
293
294	/**
295	 *	Method to show box
296	 *
297	 *	@param	array	$head       Array with properties of box title
298	 *	@param  array	$contents   Array with properties of box lines
299	 *  @param	int		$nooutput	No print, only return string
300	 *	@return	string
301	 */
302	public function showBox($head = null, $contents = null, $nooutput = 0)
303	{
304		return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
305	}
306}
307