dolibarr 21.0.0-alpha
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{
29 private $project; // @phpstan-ignore-line
30
34 public $userid;
35
39 public $socid;
40
44 public $priority;
45
51 public function __construct($db)
52 {
53 $this->db = $db;
54 }
55
56
64 public function getAllTaskByStatus($limit = 5)
65 {
66 global $user, $langs;
67
68 $sql = "SELECT";
69 $sql .= " COUNT(t.rowid), t.priority";
70 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
71 if (!$user->hasRight('societe', 'client', 'voir')) {
72 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = p.fk_soc AND sc.fk_user = ".((int) $user->id);
73 }
74 $sql .= $this->buildWhere();
75 //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
76 $sql .= " GROUP BY t.priority";
77
78 $result = array();
79
80 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
81 $resql = $this->db->query($sql);
82 if ($resql) {
83 $num = $this->db->num_rows($resql);
84 $i = 0;
85 $other = 0;
86 while ($i < $num) {
87 $row = $this->db->fetch_row($resql);
88 if ($i < $limit || $num == $limit) {
89 $result[$i] = array(
90 $row[1],
91 $row[0]
92 );
93 } else {
94 $other += $row[1];
95 }
96 $i++;
97 }
98 if ($num > $limit) {
99 $result[$i] = array(
100 $langs->transnoentitiesnoconv("Other"),
101 $other
102 );
103 }
104 $this->db->free($resql);
105 } else {
106 $this->error = "Error ".$this->db->lasterror();
107 dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
108 return -1;
109 }
110
111 return $result;
112 }
113
119 public function getAllByYear()
120 {
121 global $user;
122
123 $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
124 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
125 if (!$user->hasRight('societe', 'client', 'voir')) {
126 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = p.fk_soc AND sc.fk_user = ".((int) $user->id);
127 }
128 $sql .= $this->buildWhere();
129 $sql .= " GROUP BY year";
130 $sql .= $this->db->order('year', 'DESC');
131
132 return $this->_getAllByYear($sql);
133 }
134
135
141 public function buildWhere()
142 {
143 $sqlwhere_str = '';
144 $sqlwhere = array();
145
146 $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
147
148 if (!empty($this->userid)) {
149 $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
150 }
151 // Forced filter on socid is similar to forced filter on project. TODO Use project assignment to allow to not use filter on project
152 if (!empty($this->socid)) {
153 $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
154 }
155 if (!empty($this->year) && empty($this->month)) {
156 $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))."'";
157 }
158 if (!empty($this->year) && !empty($this->month)) {
159 $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))."'";
160 }
161 if (!empty($this->priority)) {
162 $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
163 }
164
165 if (count($sqlwhere) > 0) {
166 $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
167 }
168
169 return $sqlwhere_str;
170 }
171
179 public function getNbByMonth($year, $format = 0)
180 {
181 global $user;
182
183 $this->year = $year;
184
185 $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
186 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
187 if (!$user->hasRight('societe', 'client', 'voir')) {
188 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
189 }
190 $sql .= $this->buildWhere();
191 $sql .= " GROUP BY dm";
192 $sql .= $this->db->order('dm', 'DESC');
193
194 $res = $this->_getNbByMonth($year, $sql, $format);
195 // var_dump($res);print '<br>';
196 return $res;
197 }
198
199
207 public function getAmountByMonth($year, $format = 0)
208 {
209 // Return an empty array at the moment because task has no amount
210 return array();
211 }
212
218 protected function getAverageByMonth($year)
219 {
220 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
221 $sql .= " FROM ".$this->from;
222 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
223 $sql .= " AND ".$this->where;
224 $sql .= " GROUP BY dm";
225 $sql .= $this->db->order('dm', 'DESC');
226
227 return $this->_getAverageByMonth($year, $sql);
228 }
229}
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:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.