PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/boxes/box_graph_invoices_permonth.php

http://github.com/Dolibarr/dolibarr
PHP | 293 lines | 205 code | 36 blank | 52 comment | 38 complexity | 59f524e17365ea6906351318ad4d656e 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_invoices_permonth.php
  19. * \ingroup factures
  20. * \brief Box to show graph of invoices per month
  21. */
  22. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  23. /**
  24. * Class to manage the box to show last invoices
  25. */
  26. class box_graph_invoices_permonth extends ModeleBoxes
  27. {
  28. public $boxcode = "invoicespermonth";
  29. public $boximg = "object_bill";
  30. public $boxlabel = "BoxCustomersInvoicesPerMonth";
  31. public $depends = array("facture");
  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->facture->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.'/compta/facture/class/facture.class.php';
  63. //$facturestatic=new Facture($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. $text = $langs->trans("BoxCustomersInvoicesPerMonth", $max);
  69. $this->info_box_head = array(
  70. 'text' => $text,
  71. 'limit'=> dol_strlen($text),
  72. 'graph'=> 1,
  73. 'sublink'=>'',
  74. 'subtext'=>$langs->trans("Filter"),
  75. 'subpicto'=>'filter.png',
  76. 'subclass'=>'linkobject boxfilter',
  77. 'target'=>'none' // Set '' to get target="_blank"
  78. );
  79. $dir = ''; // We don't need a path because image file will not be saved into disk
  80. $prefix = '';
  81. $socid = 0;
  82. if ($user->socid) {
  83. $socid = $user->socid;
  84. }
  85. if (empty($user->rights->societe->client->voir) || $socid) {
  86. $prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
  87. }
  88. if ($user->rights->facture->lire) {
  89. $mesg = '';
  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.'/compta/facture/class/facturestats.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. $mode = 'customer';
  116. $WIDTH = (($shownb && $showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
  117. $HEIGHT = '192';
  118. $stats = new FactureStats($this->db, $socid, $mode, 0);
  119. // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
  120. if ($shownb) {
  121. $data1 = $stats->getNbByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
  122. $filenamenb = $dir."/".$prefix."invoicesnbinyear-".$endyear.".png";
  123. // default value for customer mode
  124. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesnbinyear-'.$endyear.'.png';
  125. $px1 = new DolGraph();
  126. $mesg = $px1->isGraphKo();
  127. if (!$mesg) {
  128. $langs->load("bills");
  129. $px1->SetData($data1);
  130. unset($data1);
  131. $i = $startyear;
  132. $legend = array();
  133. while ($i <= $endyear) {
  134. if ($startmonth != 1) {
  135. $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
  136. } else {
  137. $legend[] = $i;
  138. }
  139. $i++;
  140. }
  141. $px1->SetLegend($legend);
  142. $px1->SetMaxValue($px1->GetCeilMaxValue());
  143. $px1->SetWidth($WIDTH);
  144. $px1->SetHeight($HEIGHT);
  145. $px1->SetYLabel($langs->trans("NumberOfBills"));
  146. $px1->SetShading(3);
  147. $px1->SetHorizTickIncrement(1);
  148. $px1->SetCssPrefix("cssboxes");
  149. $px1->mode = 'depth';
  150. $px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
  151. $px1->draw($filenamenb, $fileurlnb);
  152. }
  153. }
  154. // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
  155. if ($showtot) {
  156. $data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ?-1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
  157. $filenamenb = $dir."/".$prefix."invoicesamountinyear-".$endyear.".png";
  158. // default value for customer mode
  159. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesamountinyear-'.$endyear.'.png';
  160. $px2 = new DolGraph();
  161. $mesg = $px2->isGraphKo();
  162. if (!$mesg) {
  163. $langs->load("bills");
  164. $px2->SetData($data2);
  165. unset($data2);
  166. $i = $startyear;
  167. $legend = array();
  168. while ($i <= $endyear) {
  169. if ($startmonth != 1) {
  170. $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
  171. } else {
  172. $legend[] = $i;
  173. }
  174. $i++;
  175. }
  176. $px2->SetLegend($legend);
  177. $px2->SetMaxValue($px2->GetCeilMaxValue());
  178. $px2->SetWidth($WIDTH);
  179. $px2->SetHeight($HEIGHT);
  180. $px2->SetYLabel($langs->trans("AmountOfBillsHT"));
  181. $px2->SetShading(3);
  182. $px2->SetHorizTickIncrement(1);
  183. $px2->SetCssPrefix("cssboxes");
  184. $px2->mode = 'depth';
  185. $px2->SetTitle($langs->trans("AmountOfBillsByMonthHT"));
  186. $px2->draw($filenamenb, $fileurlnb);
  187. }
  188. }
  189. if (empty($conf->use_javascript_ajax)) {
  190. $langs->load("errors");
  191. $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
  192. }
  193. if (!$mesg) {
  194. $stringtoshow = '';
  195. $stringtoshow .= '<script type="text/javascript" language="javascript">
  196. jQuery(document).ready(function() {
  197. jQuery("#idsubimg'.$this->boxcode.'").click(function() {
  198. jQuery("#idfilter'.$this->boxcode.'").toggle();
  199. });
  200. });
  201. </script>';
  202. $stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
  203. $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  204. $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
  205. $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
  206. $stringtoshow .= '<input type="hidden" name="page_y" value="">';
  207. $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
  208. $stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfBillsByMonth");
  209. $stringtoshow .= ' &nbsp; ';
  210. $stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfBillsByMonthHT");
  211. $stringtoshow .= '<br>';
  212. $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
  213. $stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
  214. $stringtoshow .= '</form>';
  215. $stringtoshow .= '</div>';
  216. if ($shownb && $showtot) {
  217. $stringtoshow .= '<div class="fichecenter">';
  218. $stringtoshow .= '<div class="fichehalfleft">';
  219. }
  220. if ($shownb) {
  221. $stringtoshow .= $px1->show();
  222. }
  223. if ($shownb && $showtot) {
  224. $stringtoshow .= '</div>';
  225. $stringtoshow .= '<div class="fichehalfright">';
  226. }
  227. if ($showtot) {
  228. $stringtoshow .= $px2->show();
  229. }
  230. if ($shownb && $showtot) {
  231. $stringtoshow .= '</div>';
  232. $stringtoshow .= '</div>';
  233. }
  234. $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover center"', 'textnoformat'=>$stringtoshow);
  235. } else {
  236. $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover left"', 'maxlength'=>500, 'text' => $mesg);
  237. }
  238. } else {
  239. $this->info_box_contents[0][0] = array(
  240. 'td' => 'class="nohover left"',
  241. 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
  242. );
  243. }
  244. }
  245. /**
  246. * Method to show box
  247. *
  248. * @param array $head Array with properties of box title
  249. * @param array $contents Array with properties of box lines
  250. * @param int $nooutput No print, only return string
  251. * @return string
  252. */
  253. public function showBox($head = null, $contents = null, $nooutput = 0)
  254. {
  255. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  256. }
  257. }