dolibarr 19.0.3
taskstats.class.php
1<?php
2/* Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
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
18include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
19include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
20
21
25class TaskStats extends Stats
26{
27 private $project;
28 public $userid;
29 public $socid;
30
34 public $priority;
35
41 public function __construct($db)
42 {
43 $this->db = $db;
44 }
45
46
54 public function getAllTaskByStatus($limit = 5)
55 {
56 global $user, $langs;
57
58 $sql = "SELECT";
59 $sql .= " COUNT(t.rowid), t.priority";
60 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
61 if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
62 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
63 }
64 $sql .= $this->buildWhere();
65 //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
66 $sql .= " GROUP BY t.priority";
67
68 $result = array();
69
70 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
71 $resql = $this->db->query($sql);
72 if ($resql) {
73 $num = $this->db->num_rows($resql);
74 $i = 0;
75 $other = 0;
76 while ($i < $num) {
77 $row = $this->db->fetch_row($resql);
78 if ($i < $limit || $num == $limit) {
79 $result[$i] = array(
80 $row[1],
81 $row[0]
82 );
83 } else {
84 $other += $row[1];
85 }
86 $i++;
87 }
88 if ($num > $limit) {
89 $result[$i] = array(
90 $langs->transnoentitiesnoconv("Other"),
91 $other
92 );
93 }
94 $this->db->free($resql);
95 } else {
96 $this->error = "Error ".$this->db->lasterror();
97 dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
98 return -1;
99 }
100
101 return $result;
102 }
103
109 public function getAllByYear()
110 {
111 global $user;
112
113 $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
114 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
115 if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
116 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
117 }
118 $sql .= $this->buildWhere();
119 $sql .= " GROUP BY year";
120 $sql .= $this->db->order('year', 'DESC');
121
122 return $this->_getAllByYear($sql);
123 }
124
125
131 public function buildWhere()
132 {
133 $sqlwhere_str = '';
134 $sqlwhere = array();
135
136 $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
137
138 if (!empty($this->userid)) {
139 $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
140 }
141 // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
142 if (!empty($this->socid)) {
143 $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
144 }
145 if (!empty($this->year) && empty($this->month)) {
146 $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->year, 1))."' AND '".$this->db->idate(dol_get_last_day($this->year, 12))."'";
147 }
148 if (!empty($this->year) && !empty($this->month)) {
149 $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->year, $this->month))."' AND '".$this->db->idate(dol_get_last_day($this->year, $this->month))."'";
150 }
151 if (!empty($this->priority)) {
152 $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
153 }
154
155 if (count($sqlwhere) > 0) {
156 $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
157 }
158
159 return $sqlwhere_str;
160 }
161
169 public function getNbByMonth($year, $format = 0)
170 {
171 global $user;
172
173 $this->year = $year;
174
175 $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
176 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
177 if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
178 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
179 }
180 $sql .= $this->buildWhere();
181 $sql .= " GROUP BY dm";
182 $sql .= $this->db->order('dm', 'DESC');
183
184 $res = $this->_getNbByMonth($year, $sql, $format);
185 // var_dump($res);print '<br>';
186 return $res;
187 }
188
189
197 public function getAmountByMonth($year, $format = 0)
198 {
199 // Return an empty array at the moment because task has no amount
200 return array();
201 }
202
208 protected function getAverageByMonth($year)
209 {
210 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
211 $sql .= " FROM ".$this->from;
212 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
213 $sql .= " AND ".$this->where;
214 $sql .= " GROUP BY dm";
215 $sql .= $this->db->order('dm', 'DESC');
216
217 return $this->_getAverageByMonth($year, $sql);
218 }
219}
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
Class to manage statistics on project tasks.
getAllTaskByStatus($limit=5)
Return all tasks grouped by status.
getAverageByMonth($year)
Return average of entity by month.
getAmountByMonth($year, $format=0)
Return the Task amount by month for a year.
buildWhere()
Build the where part.
getAllByYear()
Return count, and sum of products.
getNbByMonth($year, $format=0)
Return Task number by month for a year.
__construct($db)
Constructor of the class.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:613
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.