dolibarr  13.0.2
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 (!$user->rights->societe->client->voir && !$user->soc_id)
63  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=".$user->id;
64  $sql .= $this->buildWhere();
65  //$sql .= " AND t.fk_statut <> 0"; // We want historic also, so all task not draft
66  $sql .= " GROUP BY t.priority";
67 
68  $result = array();
69  $res = array();
70 
71  dol_syslog(get_class($this).'::'.__METHOD__."", LOG_DEBUG);
72  $resql = $this->db->query($sql);
73  if ($resql) {
74  $num = $this->db->num_rows($resql);
75  $i = 0;
76  $other = 0;
77  while ($i < $num) {
78  $row = $this->db->fetch_row($resql);
79  if ($i < $limit || $num == $limit)
80  {
81  $result[$i] = array(
82  $row[1],
83  $row[0]
84  );
85  } else $other += $row[1];
86  $i++;
87  }
88  if ($num > $limit)
89  $result[$i] = array(
90  $langs->transnoentitiesnoconv("Other"),
91  $other
92  );
93  $this->db->free($resql);
94  } else {
95  $this->error = "Error ".$this->db->lasterror();
96  dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
97  return -1;
98  }
99 
100  return $result;
101  }
102 
108  public function getAllByYear()
109  {
110  global $conf, $user, $langs;
111 
112  $datay = array();
113 
114  $wonlostfilter = 0; // No filter on status WON/LOST
115 
116  $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb";
117  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
118  if (!$user->rights->societe->client->voir && !$user->soc_id)
119  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=".$user->id;
120  $sql .= $this->buildWhere();
121  $sql .= " GROUP BY year";
122  $sql .= $this->db->order('year', 'DESC');
123 
124  return $this->_getAllByYear($sql);
125  }
126 
127 
133  public function buildWhere()
134  {
135  $sqlwhere_str = '';
136  $sqlwhere = array();
137 
138  $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
139 
140  if (!empty($this->userid))
141  $sqlwhere[] = ' t.fk_user_resp='.$this->userid;
142  // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
143  if (!empty($this->socid))
144  $sqlwhere[] = ' p.fk_soc='.$this->socid; // Link on thirdparty is on project, not on task
145  if (!empty($this->year) && empty($this->yearmonth))
146  $sqlwhere[] = " date_format(t.datec,'%Y')='".$this->db->escape($this->year)."'";
147  if (!empty($this->yearmonth))
148  $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->yearmonth))."' AND '".$this->db->idate(dol_get_last_day($this->yearmonth))."'";
149 
150  if (!empty($this->status))
151  $sqlwhere[] = " t.priority IN (".$this->priority.")";
152 
153  if (count($sqlwhere) > 0) {
154  $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
155  }
156 
157  return $sqlwhere_str;
158  }
159 
167  public function getNbByMonth($year, $format = 0)
168  {
169  global $user;
170 
171  $this->yearmonth = $year;
172 
173  $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(t.rowid) as nb";
174  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t INNER JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = t.fk_projet";
175  if (!$user->rights->societe->client->voir && !$user->soc_id)
176  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=".$user->id;
177  $sql .= $this->buildWhere();
178  $sql .= " GROUP BY dm";
179  $sql .= $this->db->order('dm', 'DESC');
180 
181  $this->yearmonth = 0;
182 
183  $res = $this->_getNbByMonth($year, $sql, $format);
184  // var_dump($res);print '<br>';
185  return $res;
186  }
187 }
getAllByYear()
Return count, and sum of products.
Parent class of statistics class.
Definition: stats.class.php:30
__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:481
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
$conf db
API class for accounts.
Definition: inc.php:54
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
buildWhere()
Build the where part.
getAllTaskByStatus($limit=5)
Return all tasks grouped by status.
Class to manage statistics on project tasks.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
Class to manage tasks.
Definition: task.class.php:35
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
getNbByMonth($year, $format=0)
Return Task number by month for a year.