taskstats.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <http://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. 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. 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 (! $user->rights->societe->client->voir && ! $user->soc_id)
  55. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
  56. $sql .= $this->buildWhere();
  57. //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
  58. $sql .= " GROUP BY t.priority";
  59. $result = array ();
  60. $res = array ();
  61. dol_syslog(get_class($this) . '::' . __METHOD__ . "", LOG_DEBUG);
  62. $resql = $this->db->query($sql);
  63. if ($resql) {
  64. $num = $this->db->num_rows($resql);
  65. $i = 0;
  66. $other = 0;
  67. while ( $i < $num ) {
  68. $row = $this->db->fetch_row($resql);
  69. if ($i < $limit || $num == $limit)
  70. {
  71. $result[$i] = array(
  72. $row[1],
  73. $row[0]
  74. );
  75. }
  76. else
  77. $other += $row[1];
  78. $i++;
  79. }
  80. if ($num > $limit)
  81. $result[$i] = array (
  82. $langs->transnoentitiesnoconv("Other"),
  83. $other
  84. );
  85. $this->db->free($resql);
  86. } else {
  87. $this->error = "Error " . $this->db->lasterror();
  88. dol_syslog(get_class($this) . '::' . __METHOD__ . ' ' . $this->error, LOG_ERR);
  89. return -1;
  90. }
  91. return $result;
  92. }
  93. /**
  94. * Return count, and sum of products
  95. *
  96. * @return array of values
  97. */
  98. function getAllByYear()
  99. {
  100. global $conf, $user, $langs;
  101. $datay = array ();
  102. $wonlostfilter=0; // No filter on status WON/LOST
  103. $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
  104. $sql.= " FROM ". MAIN_DB_PREFIX . "projet_task as t INNER JOIN " . MAIN_DB_PREFIX . "projet as p ON p.rowid = t.fk_projet";
  105. if (! $user->rights->societe->client->voir && ! $user->soc_id)
  106. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
  107. $sql.= $this->buildWhere();
  108. $sql.= " GROUP BY year";
  109. $sql.= $this->db->order('year', 'DESC');
  110. return $this->_getAllByYear($sql);
  111. }
  112. /**
  113. * Build the where part
  114. *
  115. * @return string
  116. */
  117. public function buildWhere()
  118. {
  119. $sqlwhere_str = '';
  120. $sqlwhere = array();
  121. $sqlwhere[] = ' t.entity IN (' . getEntity('project') . ')';
  122. if (! empty($this->userid))
  123. $sqlwhere[] = ' t.fk_user_resp=' . $this->userid;
  124. // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
  125. if (! empty($this->socid))
  126. $sqlwhere[] = ' p.fk_soc=' . $this->socid; // Link on thirdparty is on project, not on task
  127. if (! empty($this->year) && empty($this->yearmonth))
  128. $sqlwhere[] = " date_format(t.datec,'%Y')='" . $this->db->escape($this->year) . "'";
  129. if (! empty($this->yearmonth))
  130. $sqlwhere[] = " t.datec BETWEEN '" . $this->db->idate(dol_get_first_day($this->yearmonth)) . "' AND '" . $this->db->idate(dol_get_last_day($this->yearmonth)) . "'";
  131. if (! empty($this->status))
  132. $sqlwhere[] = " t.priority IN (" . $this->priority . ")";
  133. if (count($sqlwhere) > 0) {
  134. $sqlwhere_str = ' WHERE ' . implode(' AND ', $sqlwhere);
  135. }
  136. return $sqlwhere_str;
  137. }
  138. /**
  139. * Return Task number by month for a year
  140. *
  141. * @param int $year Year to scan
  142. * @param int $format 0=Label of absiss is a translated text, 1=Label of absiss is month number, 2=Label of absiss is first letter of month
  143. * @return array Array of values
  144. */
  145. function getNbByMonth($year, $format=0)
  146. {
  147. global $user;
  148. $this->yearmonth = $year;
  149. $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
  150. $sql.= " FROM ". MAIN_DB_PREFIX . "projet_task as t INNER JOIN " . MAIN_DB_PREFIX . "projet as p ON p.rowid = t.fk_projet";
  151. if (! $user->rights->societe->client->voir && ! $user->soc_id)
  152. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
  153. $sql .= $this->buildWhere();
  154. $sql .= " GROUP BY dm";
  155. $sql .= $this->db->order('dm', 'DESC');
  156. $this->yearmonth=0;
  157. $res = $this->_getNbByMonth($year, $sql, $format);
  158. // var_dump($res);print '<br>';
  159. return $res;
  160. }
  161. }