dolibarr 24.0.0-beta
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-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 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->db->sanitize($this->from, 0, 1, 1);
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->db->sanitize($this->from, 0, 1, 1);
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->db->sanitize($this->field).")";
127 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
128 $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape((string) $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->db->sanitize($this->field).")";
147 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
148 $sql .= " WHERE date_format(datec,'%Y') = '".$this->db->escape((string) $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->db->sanitize($this->field).") as total, avg(".$this->db->sanitize($this->field).") as avg";
164 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
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(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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)
Return number of documents per month for a given year.
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()
Return the number of tickets per year.
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to generate the form for creating a new ticket.