ticketstats.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* Copyright (C) 2016 Jean-François Ferry <hello@librethic.io>
  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 /ticket/class/ticketstats.class.php
  19. * \ingroup ticket
  20. * \brief Fichier de la classe de gestion des stats des tickets
  21. */
  22. require_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  23. require_once 'ticket.class.php';
  24. /**
  25. * Classe permettant la gestion des stats des deplacements et notes de frais
  26. */
  27. class TicketStats extends Stats
  28. {
  29. /**
  30. * @var string Name of table without prefix where object is stored
  31. */
  32. public $table_element;
  33. public $socid;
  34. public $userid;
  35. public $from;
  36. public $field;
  37. public $where;
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. * @param int $socid Id third party
  43. * @param mixed $userid Id user for filter or array of user ids
  44. * @return void
  45. */
  46. public function __construct($db, $socid = 0, $userid = 0)
  47. {
  48. global $conf;
  49. $this->db = $db;
  50. $this->socid = $socid;
  51. $this->userid = $userid;
  52. $object = new Ticket($this->db);
  53. $this->from = MAIN_DB_PREFIX.$object->table_element;
  54. $this->field = 'timing';
  55. $this->where = " fk_statut > 0";
  56. $this->where .= " AND entity = ".((int) $conf->entity);
  57. if ($this->socid > 0) {
  58. $this->where .= " AND fk_soc = ".((int) $this->socid);
  59. }
  60. if (is_array($this->userid) && count($this->userid) > 0) {
  61. $this->where .= ' AND fk_user_create IN ('.$this->db->sanitize(join(',', $this->userid)).')';
  62. } elseif ($this->userid > 0) {
  63. $this->where .= " AND fk_user_create = ".((int) $this->userid);
  64. }
  65. }
  66. /**
  67. * Renvoie le nombre de tickets par annee
  68. *
  69. * @return array Array of values
  70. */
  71. public function getNbByYear()
  72. {
  73. $sql = "SELECT YEAR(datec) as dm, count(*)";
  74. $sql .= " FROM ".$this->from;
  75. $sql .= " GROUP BY dm DESC";
  76. $sql .= " WHERE ".$this->where;
  77. return $this->_getNbByYear($sql);
  78. }
  79. /**
  80. * Return the number of tickets per month for a given year
  81. *
  82. * @param string $year Year to scan
  83. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  84. * @return array Array of values
  85. */
  86. public function getNbByMonth($year, $format = 0)
  87. {
  88. $sql = "SELECT MONTH(datec) as dm, count(*)";
  89. $sql .= " FROM ".$this->from;
  90. $sql .= " WHERE YEAR(datec) = ".((int) $year);
  91. $sql .= " AND ".$this->where;
  92. $sql .= " GROUP BY dm";
  93. $sql .= $this->db->order('dm', 'DESC');
  94. $res = $this->_getNbByMonth($year, $sql, $format);
  95. //var_dump($res);print '<br>';
  96. return $res;
  97. }
  98. /**
  99. * Return th eamount of tickets for a month and a given year
  100. *
  101. * @param int $year Year to scan
  102. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  103. * @return array Array of values
  104. */
  105. public function getAmountByMonth($year, $format = 0)
  106. {
  107. $sql = "SELECT date_format(datec,'%m') as dm, sum(".$this->field."::numeric)";
  108. $sql .= " FROM ".$this->from;
  109. $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape($year)."'";
  110. $sql .= " AND ".$this->where;
  111. $sql .= " GROUP BY dm";
  112. $sql .= $this->db->order('dm', 'DESC');
  113. $res = $this->_getAmountByMonth($year, $sql, $format);
  114. //var_dump($res);print '<br>';
  115. return $res;
  116. }
  117. /**
  118. * Return average amount
  119. *
  120. * @param int $year Year to scan
  121. * @return array Array of values
  122. */
  123. public function getAverageByMonth($year)
  124. {
  125. $sql = "SELECT date_format(datec,'%m') as dm, avg(".$this->field."::numeric)";
  126. $sql .= " FROM ".$this->from;
  127. $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape($year)."'";
  128. $sql .= " AND ".$this->where;
  129. $sql .= " GROUP BY dm";
  130. $sql .= $this->db->order('dm', 'DESC');
  131. return $this->_getAverageByMonth($year, $sql);
  132. }
  133. /**
  134. * Return nb, total and average
  135. *
  136. * @return array Array of values
  137. */
  138. public function getAllByYear()
  139. {
  140. $sql = "SELECT date_format(datec,'%Y') as year, count(*) as nb, sum(".$this->field."::numeric) as total, avg(".$this->field."::numeric) as avg";
  141. $sql .= " FROM ".$this->from;
  142. $sql .= " WHERE ".$this->where;
  143. $sql .= " GROUP BY year";
  144. $sql .= $this->db->order('year', 'DESC');
  145. return $this->_getAllByYear($sql);
  146. }
  147. }