dolibarr 21.0.0-beta
taskstats.class.php
1<?php
2/* Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
21include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
22
23
27class TaskStats extends Stats
28{
32 private $project; // @phpstan-ignore-line
33
37 public $userid;
38
42 public $socid;
43
47 public $priority;
48
54 public function __construct($db)
55 {
56 $this->db = $db;
57 }
58
59
67 public function getAllTaskByStatus($limit = 5)
68 {
69 global $user, $langs;
70
71 $sql = "SELECT";
72 $sql .= " COUNT(t.rowid), t.priority";
73 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
74 if (!$user->hasRight('societe', 'client', 'voir')) {
75 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = p.fk_soc AND sc.fk_user = ".((int) $user->id);
76 }
77 $sql .= $this->buildWhere();
78 //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
79 $sql .= " GROUP BY t.priority";
80
81 $result = array();
82
83 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
84 $resql = $this->db->query($sql);
85 if ($resql) {
86 $num = $this->db->num_rows($resql);
87 $i = 0;
88 $other = 0;
89 while ($i < $num) {
90 $row = $this->db->fetch_row($resql);
91 if ($i < $limit || $num == $limit) {
92 $result[$i] = array(
93 $row[1],
94 $row[0]
95 );
96 } else {
97 $other += $row[1];
98 }
99 $i++;
100 }
101 if ($num > $limit) {
102 $result[$i] = array(
103 $langs->transnoentitiesnoconv("Other"),
104 $other
105 );
106 }
107 $this->db->free($resql);
108 } else {
109 $this->error = "Error ".$this->db->lasterror();
110 dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
111 return -1;
112 }
113
114 return $result;
115 }
116
122 public function getAllByYear()
123 {
124 global $user;
125
126 $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
127 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
128 if (!$user->hasRight('societe', 'client', 'voir')) {
129 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = p.fk_soc AND sc.fk_user = ".((int) $user->id);
130 }
131 $sql .= $this->buildWhere();
132 $sql .= " GROUP BY year";
133 $sql .= $this->db->order('year', 'DESC');
134
135 return $this->_getAllByYear($sql);
136 }
137
138
144 public function buildWhere()
145 {
146 $sqlwhere_str = '';
147 $sqlwhere = array();
148
149 $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
150
151 if (!empty($this->userid)) {
152 $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
153 }
154 // Forced filter on socid is similar to forced filter on project. TODO Use project assignment to allow to not use filter on project
155 if (!empty($this->socid)) {
156 $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
157 }
158 if (!empty($this->year) && empty($this->month)) {
159 $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))."'";
160 }
161 if (!empty($this->year) && !empty($this->month)) {
162 $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))."'";
163 }
164 if (!empty($this->priority)) {
165 $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
166 }
167
168 if (count($sqlwhere) > 0) {
169 $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
170 }
171
172 return $sqlwhere_str;
173 }
174
182 public function getNbByMonth($year, $format = 0)
183 {
184 global $user;
185
186 $this->year = $year;
187
188 $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
189 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
190 if (!$user->hasRight('societe', 'client', 'voir')) {
191 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
192 }
193 $sql .= $this->buildWhere();
194 $sql .= " GROUP BY dm";
195 $sql .= $this->db->order('dm', 'DESC');
196
197 $res = $this->_getNbByMonth($year, $sql, $format);
198 // var_dump($res);print '<br>';
199 return $res;
200 }
201
202
210 public function getAmountByMonth($year, $format = 0)
211 {
212 // Return an empty array at the moment because task has no amount
213 return array();
214 }
215
221 protected function getAverageByMonth($year)
222 {
223 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
224 $sql .= " FROM ".$this->from;
225 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
226 $sql .= " AND ".$this->where;
227 $sql .= " GROUP BY dm";
228 $sql .= $this->db->order('dm', 'DESC');
229
230 return $this->_getAverageByMonth($year, $sql);
231 }
232}
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_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:600
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:619
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.