dolibarr 21.0.0-alpha
ticketstats.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Jean-François Ferry <hello@librethic.io>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25require_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
26require_once 'ticket.class.php';
27
28
32class TicketStats extends Stats
33{
37 public $table_element;
38
42 public $socid;
43
47 public $userid;
48
57 public function __construct($db, $socid = 0, $userid = 0)
58 {
59 global $conf;
60
61 $this->db = $db;
62 $this->socid = $socid;
63 $this->userid = $userid;
64
65 $object = new Ticket($this->db);
66 $this->from = MAIN_DB_PREFIX.$object->table_element;
67 $this->field = 'timing';
68
69 $this->where = " fk_statut > 0";
70 $this->where .= " AND entity = ".((int) $conf->entity);
71 if ($this->socid > 0) {
72 $this->where .= " AND fk_soc = ".((int) $this->socid);
73 }
74 if (is_array($this->userid) && count($this->userid) > 0) {
75 $this->where .= ' AND fk_user_create IN ('.$this->db->sanitize(implode(',', $this->userid)).')';
76 } elseif ($this->userid > 0) {
77 $this->where .= " AND fk_user_create = ".((int) $this->userid);
78 }
79 }
80
86 public function getNbByYear()
87 {
88 $sql = "SELECT YEAR(datec) as dm, count(*)";
89 $sql .= " FROM ".$this->from;
90 $sql .= " GROUP BY dm DESC";
91 $sql .= " WHERE ".$this->where;
92
93 return $this->_getNbByYear($sql);
94 }
95
103 public function getNbByMonth($year, $format = 0)
104 {
105 $sql = "SELECT MONTH(datec) as dm, count(*)";
106 $sql .= " FROM ".$this->from;
107 $sql .= " WHERE YEAR(datec) = ".((int) $year);
108 $sql .= " AND ".$this->where;
109 $sql .= " GROUP BY dm";
110 $sql .= $this->db->order('dm', 'DESC');
111
112 $res = $this->_getNbByMonth($year, $sql, $format);
113
114 return $res;
115 }
116
124 public function getAmountByMonth($year, $format = 0)
125 {
126 $sql = "SELECT date_format(datec,'%m') as dm, sum(".$this->field.")";
127 $sql .= " FROM ".$this->from;
128 $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape($year)."'";
129 $sql .= " AND ".$this->where;
130 $sql .= " GROUP BY dm";
131 $sql .= $this->db->order('dm', 'DESC');
132
133 $res = $this->_getAmountByMonth($year, $sql, $format);
134
135 return $res;
136 }
137
144 public function getAverageByMonth($year)
145 {
146 $sql = "SELECT date_format(datec,'%m') as dm, avg(".$this->field.")";
147 $sql .= " FROM ".$this->from;
148 $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape($year)."'";
149 $sql .= " AND ".$this->where;
150 $sql .= " GROUP BY dm";
151 $sql .= $this->db->order('dm', 'DESC');
152
153 return $this->_getAverageByMonth($year, $sql);
154 }
155
161 public function getAllByYear()
162 {
163 $sql = "SELECT date_format(datec,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
164 $sql .= " FROM ".$this->from;
165 $sql .= " WHERE ".$this->where;
166 $sql .= " GROUP BY year";
167 $sql .= $this->db->order('year', 'DESC');
168
169 return $this->_getAllByYear($sql);
170 }
171}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_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 the ticket stats.
getAllByYear()
Return nb, total and average.
getNbByMonth($year, $format=0)
Return the number of tickets per month for a given year.
getNbByYear()
Renvoie le nombre de tickets par annee.
getAverageByMonth($year)
Return average amount.
getAmountByMonth($year, $format=0)
Return the number of tickets for a month and a given year.
__construct($db, $socid=0, $userid=0)
Constructor.
Class to generate the form for creating a new ticket.