dolibarr  13.0.2
ticketstats.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2016 Jean-François Ferry <hello@librethic.io>
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 
23 require_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
24 require_once 'ticket.class.php';
25 
26 
30 class TicketStats extends Stats
31 {
35  public $table_element;
36 
37  public $socid;
38  public $userid;
39 
40  public $from;
41  public $field;
42  public $where;
43 
52  public function __construct($db, $socid = 0, $userid = 0)
53  {
54  global $conf;
55 
56  $this->db = $db;
57  $this->socid = $socid;
58  $this->userid = $userid;
59 
60  $object = new Ticket($this->db);
61  $this->from = MAIN_DB_PREFIX.$object->table_element;
62  $this->field = 'timing';
63 
64  $this->where = " fk_statut > 0";
65  $this->where .= " AND entity = ".$conf->entity;
66  if ($this->socid > 0) {
67  $this->where .= " AND fk_soc = ".$this->socid;
68  }
69  if (is_array($this->userid) && count($this->userid) > 0) {
70  $this->where .= ' AND fk_user_create IN ('.join(',', $this->userid).')';
71  } elseif ($this->userid > 0) {
72  $this->where .= ' AND fk_user_create = '.$this->userid;
73  }
74  }
75 
81  public function getNbByYear()
82  {
83  $sql = "SELECT YEAR(datec) as dm, count(*)";
84  $sql .= " FROM ".$this->from;
85  $sql .= " GROUP BY dm DESC";
86  $sql .= " WHERE ".$this->where;
87 
88  return $this->_getNbByYear($sql);
89  }
90 
97  public function getNbByMonth($year)
98  {
99  $sql = "SELECT MONTH(datec) as dm, count(*)";
100  $sql .= " FROM ".$this->from;
101  $sql .= " WHERE YEAR(datec) = ".$year;
102  $sql .= " AND ".$this->where;
103  $sql .= " GROUP BY dm";
104  $sql .= $this->db->order('dm', 'DESC');
105 
106  $res = $this->_getNbByMonth($year, $sql);
107  //var_dump($res);print '<br>';
108  return $res;
109  }
110 
117  public function getAmountByMonth($year)
118  {
119  $sql = "SELECT date_format(datec,'%m') as dm, sum(".$this->field.")";
120  $sql .= " FROM ".$this->from;
121  $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape($year)."'";
122  $sql .= " AND ".$this->where;
123  $sql .= " GROUP BY dm";
124  $sql .= $this->db->order('dm', 'DESC');
125 
126  $res = $this->_getAmountByMonth($year, $sql);
127  //var_dump($res);print '<br>';
128  return $res;
129  }
130 
137  public function getAverageByMonth($year)
138  {
139  $sql = "SELECT date_format(datec,'%m') as dm, avg(".$this->field.")";
140  $sql .= " FROM ".$this->from;
141  $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape($year)."'";
142  $sql .= " AND ".$this->where;
143  $sql .= " GROUP BY dm";
144  $sql .= $this->db->order('dm', 'DESC');
145 
146  return $this->_getAverageByMonth($year, $sql);
147  }
148 
154  public function getAllByYear()
155  {
156  $sql = "SELECT date_format(datec,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
157  $sql .= " FROM ".$this->from;
158  $sql .= " WHERE ".$this->where;
159  $sql .= " GROUP BY year";
160  $sql .= $this->db->order('year', 'DESC');
161 
162  return $this->_getAllByYear($sql);
163  }
164 }
Classe permettant la gestion des stats des deplacements et notes de frais.
Parent class of statistics class.
Definition: stats.class.php:30
getNbByYear()
Renvoie le nombre de tickets par annee.
_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
Class to manage ticket.
getNbByMonth($year)
Renvoie le nombre de facture par mois pour une annee donnee.
getAllByYear()
Return nb, total and average.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
getAverageByMonth($year)
Return average amount.
__construct($db, $socid=0, $userid=0)
Constructor.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
getAmountByMonth($year)
Renvoie le montant de facture par mois pour une annee donnee.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...