box_graph_invoices_peryear.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_peryear.php
  19. * \ingroup factures
  20. * \brief Box to show graph of invoices per year
  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_peryear extends ModeleBoxes
  27. {
  28. public $boxcode = "invoicesperyear";
  29. public $boximg = "object_bill";
  30. public $boxlabel = "BoxCustomersInvoicesPerYear";
  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. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. * @param string $param More parameters
  43. */
  44. public function __construct($db, $param)
  45. {
  46. global $user;
  47. $this->db = $db;
  48. $this->hidden = empty($user->rights->facture->lire);
  49. }
  50. /**
  51. * Load data into info_box_contents array to show array later.
  52. *
  53. * @param int $max Maximum number of records to load
  54. * @return void
  55. */
  56. public function loadBox($max = 5)
  57. {
  58. global $conf, $user, $langs;
  59. $this->max = $max;
  60. $refreshaction = 'refresh_'.$this->boxcode;
  61. //include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  62. //$facturestatic=new Facture($this->db);
  63. $startmonth = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1;
  64. if (empty($conf->global->GRAPH_USE_FISCAL_YEAR)) $startmonth = 1;
  65. $text = $langs->trans("Turnover", $max);
  66. $this->info_box_head = array(
  67. 'text' => $text,
  68. 'limit'=> dol_strlen($text),
  69. 'graph'=> 1,
  70. 'sublink'=>'',
  71. 'subtext'=>$langs->trans("Filter"),
  72. 'subpicto'=>'filter.png',
  73. 'subclass'=>'linkobject boxfilter',
  74. 'target'=>'none' // Set '' to get target="_blank"
  75. );
  76. $dir = ''; // We don't need a path because image file will not be saved into disk
  77. $prefix = '';
  78. $socid = 0;
  79. if ($user->socid) $socid = $user->socid;
  80. if (empty($user->rights->societe->client->voir) || $socid) $prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
  81. if ($user->rights->facture->lire) {
  82. $mesg = '';
  83. $param_year = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
  84. $param_showtot = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showtot';
  85. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  86. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
  87. $autosetarray = preg_split("/[,;:]+/", GETPOST('DOL_AUTOSET_COOKIE'));
  88. if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode, $autosetarray)) {
  89. $endyear = GETPOST($param_year, 'int');
  90. $showtot = GETPOST($param_showtot, 'alpha');
  91. } else {
  92. $tmparray = json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode], true);
  93. $endyear = $tmparray['year'];
  94. $showtot = $tmparray['showtot'];
  95. }
  96. if (empty($showtot)) { $showtot = 1; }
  97. $nowarray = dol_getdate(dol_now(), true);
  98. if (empty($endyear)) $endyear = $nowarray['year'];
  99. $numberyears = (empty($conf->global->MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH) ? 5 : $conf->global->MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH);
  100. $startyear = $endyear - $numberyears;
  101. $mode = 'customer';
  102. $WIDTH = (($showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
  103. $HEIGHT = '192';
  104. $stats = new FactureStats($this->db, $socid, $mode, 0);
  105. // Build graphic amount of object. $data = array(array('Lib',val1,val2,val3),...)
  106. $data2 = $stats->getAmountByYear($numberyears);
  107. $filenamenb = $dir."/".$prefix."invoicesamountyears-".$endyear.".png";
  108. // default value for customer mode
  109. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesamountyears-'.$endyear.'.png';
  110. if ($mode == 'supplier') {
  111. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicessupplieramountyears-'.$endyear.'.png';
  112. }
  113. $px2 = new DolGraph();
  114. $mesg = $px2->isGraphKo();
  115. if (!$mesg) {
  116. $langs->load("bills");
  117. $px2->SetData($data2);
  118. unset($data2);
  119. $i = $startyear;
  120. $legend = array();
  121. while ($i <= $endyear) {
  122. if ($startmonth != 1) {
  123. $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
  124. } else {
  125. $legend[] = $i;
  126. }
  127. $i++;
  128. }
  129. $px2->SetLegend([$langs->trans("AmountOfBillsHT")]);
  130. $px2->SetMaxValue($px2->GetCeilMaxValue());
  131. $px2->SetWidth($WIDTH);
  132. $px2->SetHeight($HEIGHT);
  133. $px2->SetYLabel($langs->trans("AmountOfBillsHT"));
  134. $px2->SetShading(3);
  135. $px2->SetHorizTickIncrement(1);
  136. $px2->SetCssPrefix("cssboxes");
  137. $px2->mode = 'depth';
  138. $px2->SetTitle($langs->trans("Turnover"));
  139. $px2->draw($filenamenb, $fileurlnb);
  140. }
  141. if (empty($conf->use_javascript_ajax)) {
  142. $langs->load("errors");
  143. $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
  144. }
  145. if (!$mesg) {
  146. $stringtoshow = '';
  147. $stringtoshow .= '<script type="text/javascript" language="javascript">
  148. jQuery(document).ready(function() {
  149. jQuery("#idsubimg'.$this->boxcode.'").click(function() {
  150. jQuery("#idfilter'.$this->boxcode.'").toggle();
  151. });
  152. });
  153. </script>';
  154. $stringtoshow .= '<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
  155. $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  156. $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
  157. $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
  158. $stringtoshow .= '<input type="hidden" name="page_y" value="">';
  159. $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,showtot">';
  160. $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
  161. $stringtoshow .= '<input class="reposition inline-block valigntextbottom" type="image" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
  162. $stringtoshow .= '</form>';
  163. $stringtoshow .= '</div>';
  164. $stringtoshow .= $px2->show();
  165. $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover center"', 'textnoformat'=>$stringtoshow);
  166. } else {
  167. $this->info_box_contents[0][0] = array('tr'=>'class="oddeven nohover"', 'td' => 'class="nohover left"', 'maxlength'=>500, 'text' => $mesg);
  168. }
  169. } else {
  170. $this->info_box_contents[0][0] = array(
  171. 'td' => 'class="nohover left"',
  172. 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
  173. );
  174. }
  175. }
  176. /**
  177. * Method to show box
  178. *
  179. * @param array $head Array with properties of box title
  180. * @param array $contents Array with properties of box lines
  181. * @param int $nooutput No print, only return string
  182. * @return string
  183. */
  184. public function showBox($head = null, $contents = null, $nooutput = 0)
  185. {
  186. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  187. }
  188. }