dolibarr 21.0.0-alpha
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
42 public $socid;
43 public $userid;
44
45 public $from;
46 public $field;
47 public $where;
48
49
58 public function __construct($db, $socid, $mode, $userid = 0)
59 {
60 global $user, $conf;
61
62 $this->db = $db;
63
64 $this->socid = ($socid > 0 ? $socid : 0);
65 $this->userid = $userid;
66 $this->cachefilesuffix = $mode;
67
68 if ($mode == 'customer') {
69 $object = new Fichinter($this->db);
70 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
71 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
72 $this->field = '0';
73 $this->field_line = '0';
74 //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
75 }
76 if (!$user->hasRight('societe', 'client', 'voir')) {
77 $this->where .= (!empty($this->where) ? ' AND ' : '')." c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
78 }
79 $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('intervention').')';
80
81 if ($this->socid) {
82 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
83 }
84 if ($this->userid > 0) {
85 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
86 }
87 }
88
96 public function getNbByMonth($year, $format = 0)
97 {
98 global $user;
99
100 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
101 $sql .= " FROM ".$this->from;
102 if (!$user->hasRight('societe', 'client', 'voir')) {
103 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
104 }
105 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
106 $sql .= " AND ".$this->where;
107 $sql .= " GROUP BY dm";
108 $sql .= $this->db->order('dm', 'DESC');
109
110 $res = $this->_getNbByMonth($year, $sql, $format);
111 return $res;
112 }
113
120 public function getNbByYear()
121 {
122 global $user;
123
124 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
125 $sql .= " FROM ".$this->from;
126 if (!$user->hasRight('societe', 'client', 'voir')) {
127 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
128 }
129 $sql .= " WHERE ".$this->where;
130 $sql .= " GROUP BY dm";
131 $sql .= $this->db->order('dm', 'DESC');
132
133 return $this->_getNbByYear($sql);
134 }
135
143 public function getAmountByMonth($year, $format = 0)
144 {
145 global $user;
146
147 $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
148 $sql .= " FROM ".$this->from;
149 if (!$user->hasRight('societe', 'client', 'voir')) {
150 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
151 }
152 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
153 $sql .= " AND ".$this->where;
154 $sql .= " GROUP BY dm";
155 $sql .= $this->db->order('dm', 'DESC');
156
157 $res = $this->_getAmountByMonth($year, $sql, $format);
158 return $res;
159 }
160
167 public function getAverageByMonth($year)
168 {
169 global $user;
170
171 $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
172 $sql .= " FROM ".$this->from;
173 if (!$user->hasRight('societe', 'client', 'voir')) {
174 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
175 }
176 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
177 $sql .= " AND ".$this->where;
178 $sql .= " GROUP BY dm";
179 $sql .= $this->db->order('dm', 'DESC');
180
181 return $this->_getAverageByMonth($year, $sql);
182 }
183
189 public function getAllByYear()
190 {
191 global $user;
192
193 $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
194 $sql .= " FROM ".$this->from;
195 if (!$user->hasRight('societe', 'client', 'voir')) {
196 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
197 }
198 $sql .= " WHERE ".$this->where;
199 $sql .= " GROUP BY year";
200 $sql .= $this->db->order('year', 'DESC');
201
202 return $this->_getAllByYear($sql);
203 }
204
212 public function getAllByProduct($year, $limit = 0)
213 {
214 global $user;
215
216 $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
217 $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
218 //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
219 $sql .= " WHERE ".$this->where;
220 $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
221 $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))."'";
222 $sql .= " GROUP BY product.ref";
223 $sql .= $this->db->order('nb', 'DESC');
224 //$sql.= $this->db->plimit(20);
225
226 return $this->_getAllByProduct($sql, $limit);
227 }
228}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.