dolibarr  16.0.5
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 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
19 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
20 
21 
25 class TaskStats extends Stats
26 {
27  private $project;
28  public $userid;
29  public $socid;
30  public $year;
31 
37  public function __construct($db)
38  {
39  $this->db = $db;
40 
41  require_once 'task.class.php';
42  $this->task = new Task($this->db);
43  }
44 
45 
53  public function getAllTaskByStatus($limit = 5)
54  {
55  global $conf, $user, $langs;
56 
57  $datay = array();
58 
59  $sql = "SELECT";
60  $sql .= " COUNT(t.rowid), t.priority";
61  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
62  if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
63  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
64  }
65  $sql .= $this->buildWhere();
66  //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
67  $sql .= " GROUP BY t.priority";
68 
69  $result = array();
70  $res = array();
71 
72  dol_syslog(get_class($this).'::'.__METHOD__."", LOG_DEBUG);
73  $resql = $this->db->query($sql);
74  if ($resql) {
75  $num = $this->db->num_rows($resql);
76  $i = 0;
77  $other = 0;
78  while ($i < $num) {
79  $row = $this->db->fetch_row($resql);
80  if ($i < $limit || $num == $limit) {
81  $result[$i] = array(
82  $row[1],
83  $row[0]
84  );
85  } else {
86  $other += $row[1];
87  }
88  $i++;
89  }
90  if ($num > $limit) {
91  $result[$i] = array(
92  $langs->transnoentitiesnoconv("Other"),
93  $other
94  );
95  }
96  $this->db->free($resql);
97  } else {
98  $this->error = "Error ".$this->db->lasterror();
99  dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
100  return -1;
101  }
102 
103  return $result;
104  }
105 
111  public function getAllByYear()
112  {
113  global $conf, $user, $langs;
114 
115  $datay = array();
116 
117  $wonlostfilter = 0; // No filter on status WON/LOST
118 
119  $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
120  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
121  if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
122  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
123  }
124  $sql .= $this->buildWhere();
125  $sql .= " GROUP BY year";
126  $sql .= $this->db->order('year', 'DESC');
127 
128  return $this->_getAllByYear($sql);
129  }
130 
131 
137  public function buildWhere()
138  {
139  $sqlwhere_str = '';
140  $sqlwhere = array();
141 
142  $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
143 
144  if (!empty($this->userid)) {
145  $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
146  }
147  // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
148  if (!empty($this->socid)) {
149  $sqlwhere[] = ' p.fk_soc = '.((int) $this->socid); // Link on thirdparty is on project, not on task
150  }
151  if (!empty($this->year) && empty($this->yearmonth)) {
152  $sqlwhere[] = " date_format(t.datec,'%Y')='".$this->db->escape($this->year)."'";
153  }
154  if (!empty($this->yearmonth)) {
155  $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->yearmonth))."' AND '".$this->db->idate(dol_get_last_day($this->yearmonth))."'";
156  }
157  if (!empty($this->priority)) {
158  $sqlwhere[] = " t.priority IN (".$this->db->sanitize($this->priority, 1).")";
159  }
160 
161  if (count($sqlwhere) > 0) {
162  $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
163  }
164 
165  return $sqlwhere_str;
166  }
167 
175  public function getNbByMonth($year, $format = 0)
176  {
177  global $user;
178 
179  $this->yearmonth = $year;
180 
181  $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
182  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
183  if (empty($user->rights->societe->client->voir) && !$user->soc_id) {
184  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=p.fk_soc AND sc.fk_user=".((int) $user->id);
185  }
186  $sql .= $this->buildWhere();
187  $sql .= " GROUP BY dm";
188  $sql .= $this->db->order('dm', 'DESC');
189 
190  $this->yearmonth = 0;
191 
192  $res = $this->_getNbByMonth($year, $sql, $format);
193  // var_dump($res);print '<br>';
194  return $res;
195  }
196 
204  public function getAmountByMonth($year, $format = 0)
205  {
206  // Return an empty array at the moment because task has no amount
207  return array();
208  }
209 
215  protected function getAverageByMonth($year)
216  {
217  $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
218  $sql .= " FROM ".$this->from;
219  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
220  $sql .= " AND ".$this->where;
221  $sql .= " GROUP BY dm";
222  $sql .= $this->db->order('dm', 'DESC');
223 
224  return $this->_getAverageByMonth($year, $sql);
225  }
226 }
TaskStats\getAllByYear
getAllByYear()
Return count, and sum of products.
Definition: taskstats.class.php:111
db
$conf db
API class for accounts.
Definition: inc.php:41
TaskStats\__construct
__construct($db)
Constructor of the class.
Definition: taskstats.class.php:37
Task
Class to manage tasks.
Definition: task.class.php:37
Stats
Parent class of statistics class.
Definition: stats.class.php:30
TaskStats
Class to manage statistics on project tasks.
Definition: taskstats.class.php:25
Stats\_getAllByYear
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Definition: stats.class.php:384
TaskStats\getAverageByMonth
getAverageByMonth($year)
Return average of entity by month.
Definition: taskstats.class.php:215
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
TaskStats\getAllTaskByStatus
getAllTaskByStatus($limit=5)
Return all tasks grouped by status.
Definition: taskstats.class.php:53
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
TaskStats\getAmountByMonth
getAmountByMonth($year, $format=0)
Return the Task amount by month for a year.
Definition: taskstats.class.php:204
TaskStats\buildWhere
buildWhere()
Build the where part.
Definition: taskstats.class.php:137
Stats\_getNbByMonth
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
Definition: stats.class.php:435
TaskStats\getNbByMonth
getNbByMonth($year, $format=0)
Return Task number by month for a year.
Definition: taskstats.class.php:175
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Stats\_getAverageByMonth
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
Definition: stats.class.php:550