PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/boxes/box_graph_propales_permonth.php

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