dolibarr 21.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 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
29include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30
31
35class FichinterStats extends Stats
36{
40 public $table_element;
41
45 public $socid;
49 public $userid;
50
54 public $from;
58 public $field;
62 public $where;
63
64
73 public function __construct($db, $socid, $mode, $userid = 0)
74 {
75 global $user, $conf;
76
77 $this->db = $db;
78
79 $this->socid = ($socid > 0 ? $socid : 0);
80 $this->userid = $userid;
81 $this->cachefilesuffix = $mode;
82
83 if ($mode == 'customer') {
84 $object = new Fichinter($this->db);
85 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
86 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
87 $this->field = '0';
88 $this->field_line = '0';
89 //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
90 }
91 if (!$user->hasRight('societe', 'client', 'voir')) {
92 $this->where .= (!empty($this->where) ? ' AND ' : '')." c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
93 }
94 $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('intervention').')';
95
96 if ($this->socid) {
97 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
98 }
99 if ($this->userid > 0) {
100 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
101 }
102 }
103
111 public function getNbByMonth($year, $format = 0)
112 {
113 global $user;
114
115 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
116 $sql .= " FROM ".$this->from;
117 if (!$user->hasRight('societe', 'client', 'voir')) {
118 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
119 }
120 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
121 $sql .= " AND ".$this->where;
122 $sql .= " GROUP BY dm";
123 $sql .= $this->db->order('dm', 'DESC');
124
125 $res = $this->_getNbByMonth($year, $sql, $format);
126 return $res;
127 }
128
135 public function getNbByYear()
136 {
137 global $user;
138
139 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
140 $sql .= " FROM ".$this->from;
141 if (!$user->hasRight('societe', 'client', 'voir')) {
142 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
143 }
144 $sql .= " WHERE ".$this->where;
145 $sql .= " GROUP BY dm";
146 $sql .= $this->db->order('dm', 'DESC');
147
148 return $this->_getNbByYear($sql);
149 }
150
158 public function getAmountByMonth($year, $format = 0)
159 {
160 global $user;
161
162 $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
163 $sql .= " FROM ".$this->from;
164 if (!$user->hasRight('societe', 'client', 'voir')) {
165 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
166 }
167 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
168 $sql .= " AND ".$this->where;
169 $sql .= " GROUP BY dm";
170 $sql .= $this->db->order('dm', 'DESC');
171
172 $res = $this->_getAmountByMonth($year, $sql, $format);
173 return $res;
174 }
175
182 public function getAverageByMonth($year)
183 {
184 global $user;
185
186 $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
187 $sql .= " FROM ".$this->from;
188 if (!$user->hasRight('societe', 'client', 'voir')) {
189 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
190 }
191 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
192 $sql .= " AND ".$this->where;
193 $sql .= " GROUP BY dm";
194 $sql .= $this->db->order('dm', 'DESC');
195
196 return $this->_getAverageByMonth($year, $sql);
197 }
198
204 public function getAllByYear()
205 {
206 global $user;
207
208 $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
209 $sql .= " FROM ".$this->from;
210 if (!$user->hasRight('societe', 'client', 'voir')) {
211 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
212 }
213 $sql .= " WHERE ".$this->where;
214 $sql .= " GROUP BY year";
215 $sql .= $this->db->order('year', 'DESC');
216
217 return $this->_getAllByYear($sql);
218 }
219
227 public function getAllByProduct($year, $limit = 0)
228 {
229 global $user;
230
231 $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
232 $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
233 //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
234 $sql .= " WHERE ".$this->where;
235 $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
236 $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))."'";
237 $sql .= " GROUP BY product.ref";
238 $sql .= $this->db->order('nb', 'DESC');
239 //$sql.= $this->db->plimit(20);
240
241 return $this->_getAllByProduct($sql, $limit);
242 }
243}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:600
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:619
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79