dolibarr 24.0.0-beta
fichinterstats.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2012 Marcos GarcĂ­a <marcosgdf@gmail.com>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
29include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31
32
36class FichinterStats extends Stats
37{
41 public $table_element;
42
46 public $socid;
50 public $userid;
51
55 public $from;
59 public $field;
63 public $where;
64
65
74 public function __construct($db, $socid, $mode, $userid = 0)
75 {
76 global $user, $conf;
77
78 $this->db = $db;
79
80 $this->socid = ($socid > 0 ? $socid : 0);
81 $this->userid = $userid;
82 $this->cachefilesuffix = $mode;
83
84 if ($mode == 'customer') {
85 $object = new Fichinter($this->db);
86 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
87 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
88 $this->field = '0';
89 $this->field_line = '0';
90 //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
91 }
92
93 $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('intervention').')';
94
95 if ($this->socid) {
96 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
97 }
98 if ($this->userid > 0) {
99 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
100 }
101 }
102
110 public function getNbByMonth($year, $format = 0)
111 {
112 global $user;
113
114 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
115 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
116 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
117 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
118 }
119 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
120 $sql .= " AND ".$this->where;
121 $sql .= " GROUP BY dm";
122 $sql .= $this->db->order('dm', 'DESC');
123
124 $res = $this->_getNbByMonth($year, $sql, $format);
125 return $res;
126 }
127
133 public function getNbByYear()
134 {
135 global $user;
136
137 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
138 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
139 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
140 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
141 }
142 $sql .= " WHERE ".$this->where;
143 $sql .= " GROUP BY dm";
144 $sql .= $this->db->order('dm', 'DESC');
145
146 return $this->_getNbByYear($sql);
147 }
148
156 public function getAmountByMonth($year, $format = 0)
157 {
158 global $user;
159
160 $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
161 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
162 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
163 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
164 }
165 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
166 $sql .= " AND ".$this->where;
167 $sql .= " GROUP BY dm";
168 $sql .= $this->db->order('dm', 'DESC');
169
170 $res = $this->_getAmountByMonth($year, $sql, $format);
171 return $res;
172 }
173
180 public function getAverageByMonth($year)
181 {
182 global $user;
183
184 $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
185 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
186 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
187 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
188 }
189 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
190 $sql .= " AND ".$this->where;
191 $sql .= " GROUP BY dm";
192 $sql .= $this->db->order('dm', 'DESC');
193
194 return $this->_getAverageByMonth($year, $sql);
195 }
196
202 public function getAllByYear()
203 {
204 global $user;
205
206 $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
207 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
208 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
209 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
210 }
211 $sql .= " WHERE ".$this->where;
212 $sql .= " GROUP BY year";
213 $sql .= $this->db->order('year', 'DESC');
214
215 return $this->_getAllByYear($sql);
216 }
217
225 public function getAllByProduct($year, $limit = 0)
226 {
227 $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
228 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1).",";
229 $sql .= " ".$this->db->sanitize($this->from_line, 0, 1, 1).",";
230 $sql .= " ".MAIN_DB_PREFIX."product as product";
231 //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
232 $sql .= " WHERE ".$this->where;
233 $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
234 $sql .= " AND c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
235 $sql .= " GROUP BY product.ref";
236 $sql .= $this->db->order('nb', 'DESC');
237 //$sql.= $this->db->plimit(20);
238
239 return $this->_getAllByProduct($sql, $limit);
240 }
241}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage intervention statistics.
__construct($db, $socid, $mode, $userid=0)
Constructor.
getNbByYear()
Return interventions number per year.
getAmountByMonth($year, $format=0)
Return the intervention amount by month for a year.
getAverageByMonth($year)
Return the intervention amount average by month for a year.
getAllByYear()
Return nb, total and average.
getNbByMonth($year, $format=0)
Return intervention number by month for a year.
getAllByProduct($year, $limit=0)
Return nb, amount of predefined product for year.
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.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getNbByMonth($year, $sql, $format=0)
Return number of documents per month for a given year.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:604
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:623
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.