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