taskstats.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* Lead
  3. * Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  19. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  20. /**
  21. * Class to manage statistics on project tasks
  22. */
  23. class TaskStats extends Stats
  24. {
  25. private $project;
  26. public $userid;
  27. public $socid;
  28. public $year;
  29. /**
  30. * Constructor of the class
  31. *
  32. * @param DoliDb $db Database handler
  33. */
  34. public function __construct($db)
  35. {
  36. $this->db = $db;
  37. require_once 'task.class.php';
  38. $this->task = new Task($this->db);
  39. }
  40. /**
  41. * Return all tasks grouped by status.
  42. *
  43. * @param int $limit Limit results
  44. * @return array|int Array with value or -1 if error
  45. * @throws Exception
  46. */
  47. public function getAllTaskByStatus($limit = 5)
  48. {
  49. global $conf, $user, $langs;
  50. $datay = array();
  51. $sql = "SELECT";
  52. $sql .= " COUNT(t.rowid), t.priority";
  53. $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
  54. if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
  55. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
  56. }
  57. $sql .= $this->buildWhere();
  58. //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
  59. $sql .= " GROUP BY t.priority";
  60. $result = array();
  61. $res = array();
  62. dol_syslog(get_class($this).'::'.__METHOD__."", LOG_DEBUG);
  63. $resql = $this->db->query($sql);
  64. if ($resql) {
  65. $num = $this->db->num_rows($resql);
  66. $i = 0;
  67. $other = 0;
  68. while ($i < $num) {
  69. $row = $this->db->fetch_row($resql);
  70. if ($i < $limit || $num == $limit) {
  71. $result[$i] = array(
  72. $row[1],
  73. $row[0]
  74. );
  75. } else {
  76. $other += $row[1];
  77. }
  78. $i++;
  79. }
  80. if ($num > $limit) {
  81. $result[$i] = array(
  82. $langs->transnoentitiesnoconv("Other"),
  83. $other
  84. );
  85. }
  86. $this->db->free($resql);
  87. } else {
  88. $this->error = "Error ".$this->db->lasterror();
  89. dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
  90. return -1;
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Return count, and sum of products
  96. *
  97. * @return array of values
  98. */
  99. public function getAllByYear()
  100. {
  101. global $conf, $user, $langs;
  102. $datay = array();
  103. $wonlostfilter = 0; // No filter on status WON/LOST
  104. $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
  105. $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
  106. if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
  107. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
  108. }
  109. $sql .= $this->buildWhere();
  110. $sql .= " GROUP BY year";
  111. $sql .= $this->db->order('year', 'DESC');
  112. return $this->_getAllByYear($sql);
  113. }
  114. /**
  115. * Build the where part
  116. *
  117. * @return string
  118. */
  119. public function buildWhere()
  120. {
  121. $sqlwhere_str = '';
  122. $sqlwhere = array();
  123. $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
  124. if (!empty($this->userid)) {
  125. $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
  126. }
  127. // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
  128. if (!empty($this->socid)) {
  129. $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
  130. }
  131. if (!empty($this->year) && empty($this->yearmonth)) {
  132. $sqlwhere[] = " date_format(t.datec,'%Y')='".$this->db->escape($this->year)."'";
  133. }
  134. if (!empty($this->yearmonth)) {
  135. $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->yearmonth))."' AND '".$this->db->idate(dol_get_last_day($this->yearmonth))."'";
  136. }
  137. if (!empty($this->priority)) {
  138. $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
  139. }
  140. if (count($sqlwhere) > 0) {
  141. $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
  142. }
  143. return $sqlwhere_str;
  144. }
  145. /**
  146. * Return Task number by month for a year
  147. *
  148. * @param int $year Year to scan
  149. * @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
  150. * @return array Array of values
  151. */
  152. public function getNbByMonth($year, $format = 0)
  153. {
  154. global $user;
  155. $this->yearmonth = $year;
  156. $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
  157. $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
  158. if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
  159. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
  160. }
  161. $sql .= $this->buildWhere();
  162. $sql .= " GROUP BY dm";
  163. $sql .= $this->db->order('dm', 'DESC');
  164. $this->yearmonth = 0;
  165. $res = $this->_getNbByMonth($year, $sql, $format);
  166. // var_dump($res);print '<br>';
  167. return $res;
  168. }
  169. /**
  170. * Return the Task amount by month for a year
  171. *
  172. * @param int $year Year to scan
  173. * @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
  174. * @return array Array with amount by month
  175. */
  176. public function getAmountByMonth($year, $format = 0)
  177. {
  178. // Return an empty array at the moment because task has no amount
  179. return array();
  180. }
  181. /**
  182. * Return average of entity by month
  183. * @param int $year year number
  184. * @return int value
  185. */
  186. protected function getAverageByMonth($year)
  187. {
  188. $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
  189. $sql .= " FROM ".$this->from;
  190. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  191. $sql .= " AND ".$this->where;
  192. $sql .= " GROUP BY dm";
  193. $sql .= $this->db->order('dm', 'DESC');
  194. return $this->_getAverageByMonth($year, $sql);
  195. }
  196. }