dolibarr  17.0.4
taskstats.class.php
1 <?php
2 /* Lead
3  * Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
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 
19 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
20 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
21 
22 
26 class TaskStats extends Stats
27 {
28  private $project;
29  public $userid;
30  public $socid;
31  public $year;
32 
38  public function __construct($db)
39  {
40  $this->db = $db;
41 
42  require_once 'task.class.php';
43  $this->task = new Task($this->db);
44  }
45 
46 
54  public function getAllTaskByStatus($limit = 5)
55  {
56  global $conf, $user, $langs;
57 
58  $datay = array();
59 
60  $sql = "SELECT";
61  $sql .= " COUNT(t.rowid), t.priority";
62  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
63  if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
64  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
65  }
66  $sql .= $this->buildWhere();
67  //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
68  $sql .= " GROUP BY t.priority";
69 
70  $result = array();
71  $res = array();
72 
73  dol_syslog(get_class($this).'::'.__METHOD__."", LOG_DEBUG);
74  $resql = $this->db->query($sql);
75  if ($resql) {
76  $num = $this->db->num_rows($resql);
77  $i = 0;
78  $other = 0;
79  while ($i < $num) {
80  $row = $this->db->fetch_row($resql);
81  if ($i < $limit || $num == $limit) {
82  $result[$i] = array(
83  $row[1],
84  $row[0]
85  );
86  } else {
87  $other += $row[1];
88  }
89  $i++;
90  }
91  if ($num > $limit) {
92  $result[$i] = array(
93  $langs->transnoentitiesnoconv("Other"),
94  $other
95  );
96  }
97  $this->db->free($resql);
98  } else {
99  $this->error = "Error ".$this->db->lasterror();
100  dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
101  return -1;
102  }
103 
104  return $result;
105  }
106 
112  public function getAllByYear()
113  {
114  global $conf, $user, $langs;
115 
116  $datay = array();
117 
118  $wonlostfilter = 0; // No filter on status WON/LOST
119 
120  $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
121  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
122  if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
123  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
124  }
125  $sql .= $this->buildWhere();
126  $sql .= " GROUP BY year";
127  $sql .= $this->db->order('year', 'DESC');
128 
129  return $this->_getAllByYear($sql);
130  }
131 
132 
138  public function buildWhere()
139  {
140  $sqlwhere_str = '';
141  $sqlwhere = array();
142 
143  $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
144 
145  if (!empty($this->userid)) {
146  $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
147  }
148  // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
149  if (!empty($this->socid)) {
150  $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
151  }
152  if (!empty($this->year) && empty($this->yearmonth)) {
153  $sqlwhere[] = " date_format(t.datec,'%Y')='".$this->db->escape($this->year)."'";
154  }
155  if (!empty($this->yearmonth)) {
156  $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->yearmonth))."' AND '".$this->db->idate(dol_get_last_day($this->yearmonth))."'";
157  }
158  if (!empty($this->priority)) {
159  $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
160  }
161 
162  if (count($sqlwhere) > 0) {
163  $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
164  }
165 
166  return $sqlwhere_str;
167  }
168 
176  public function getNbByMonth($year, $format = 0)
177  {
178  global $user;
179 
180  $this->yearmonth = $year;
181 
182  $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
183  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
184  if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
185  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
186  }
187  $sql .= $this->buildWhere();
188  $sql .= " GROUP BY dm";
189  $sql .= $this->db->order('dm', 'DESC');
190 
191  $this->yearmonth = 0;
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.
Definition: stats.class.php:31
_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 tasks.
Definition: task.class.php:38
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:575
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:594
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41