dolibarr 18.0.6
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 public $year;
31 public $month;
32
38 public function __construct($db)
39 {
40 $this->db = $db;
41 }
42
43
51 public function getAllTaskByStatus($limit = 5)
52 {
53 global $user, $langs;
54
55 $sql = "SELECT";
56 $sql .= " COUNT(t.rowid), t.priority";
57 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
58 if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
59 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
60 }
61 $sql .= $this->buildWhere();
62 //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
63 $sql .= " GROUP BY t.priority";
64
65 $result = array();
66
67 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
68 $resql = $this->db->query($sql);
69 if ($resql) {
70 $num = $this->db->num_rows($resql);
71 $i = 0;
72 $other = 0;
73 while ($i < $num) {
74 $row = $this->db->fetch_row($resql);
75 if ($i < $limit || $num == $limit) {
76 $result[$i] = array(
77 $row[1],
78 $row[0]
79 );
80 } else {
81 $other += $row[1];
82 }
83 $i++;
84 }
85 if ($num > $limit) {
86 $result[$i] = array(
87 $langs->transnoentitiesnoconv("Other"),
88 $other
89 );
90 }
91 $this->db->free($resql);
92 } else {
93 $this->error = "Error ".$this->db->lasterror();
94 dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
95 return -1;
96 }
97
98 return $result;
99 }
100
106 public function getAllByYear()
107 {
108 global $user;
109
110 $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
111 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
112 if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
113 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
114 }
115 $sql .= $this->buildWhere();
116 $sql .= " GROUP BY year";
117 $sql .= $this->db->order('year', 'DESC');
118
119 return $this->_getAllByYear($sql);
120 }
121
122
128 public function buildWhere()
129 {
130 $sqlwhere_str = '';
131 $sqlwhere = array();
132
133 $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
134
135 if (!empty($this->userid)) {
136 $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
137 }
138 // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
139 if (!empty($this->socid)) {
140 $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
141 }
142 if (!empty($this->year) && empty($this->month)) {
143 $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))."'";
144 }
145 if (!empty($this->year) && !empty($this->month)) {
146 $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))."'";
147 }
148 if (!empty($this->priority)) {
149 $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
150 }
151
152 if (count($sqlwhere) > 0) {
153 $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
154 }
155
156 return $sqlwhere_str;
157 }
158
166 public function getNbByMonth($year, $format = 0)
167 {
168 global $user;
169
170 $this->year = $year;
171
172 $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
173 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
174 if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
175 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
176 }
177 $sql .= $this->buildWhere();
178 $sql .= " GROUP BY dm";
179 $sql .= $this->db->order('dm', 'DESC');
180
181 $res = $this->_getNbByMonth($year, $sql, $format);
182 // var_dump($res);print '<br>';
183 return $res;
184 }
185
186
194 public function getAmountByMonth($year, $format = 0)
195 {
196 // Return an empty array at the moment because task has no amount
197 return array();
198 }
199
205 protected function getAverageByMonth($year)
206 {
207 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
208 $sql .= " FROM ".$this->from;
209 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
210 $sql .= " AND ".$this->where;
211 $sql .= " GROUP BY dm";
212 $sql .= $this->db->order('dm', 'DESC');
213
214 return $this->_getAverageByMonth($year, $sql);
215 }
216}
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:577
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:596
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.