dolibarr 24.0.0-beta
myobjectstats.class.php
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) 2020 Maxime DEMAREST <maxime@indelog.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31dol_include_once('/mymodule/class/myobject.class.php');
32
33
37class MyObjectStats extends Stats
38{
42 public $table_element;
43
47 public $socid;
48
52 public $userid;
53
57 public $from;
58
62 public $from_line;
63
67 public $field;
68
72 public $field_line;
73
77 public $categ_link;
78
82 public $where = '';
83
87 public $join;
88
89
99 public function __construct($db, $socid, $mode, $userid = 0, $categid = 0)
100 {
101 global $hookmanager;
102 $this->db = $db;
103
104 $this->socid = ($socid > 0 ? $socid : 0);
105 $this->userid = $userid;
106 $this->cachefilesuffix = $mode;
107 $this->join = '';
108
109 $object = new MyObject($this->db);
110 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
111 if ($object->ismultientitymanaged && (string) $object->ismultientitymanaged != '1') {
112 // $object->ismultientitymanaged is 'field@table'
113 $tmparray = explode('@', (string) $object->ismultientitymanaged);
114 $field = $tmparray[0];
115 $table = $tmparray[1];
116 $this->from = " INNER JOIN ".MAIN_DB_PREFIX.$table." as e ON c.".$this->db->sanitize($field)." = e.rowid AND e.entity IN (".getEntity($table.'@mymodule').")";
117 }
118 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
119 $this->field = 'total_ht';
120 $this->field_line = 'total_ht';
121 //$this->where .= " c.status > 0"; // Not draft and not cancelled
122 $this->categ_link = MAIN_DB_PREFIX.'categorie_societe';
123 if ((string) $object->ismultientitymanaged == '1') {
124 $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('myobject@mymodule').")";
125 }
126 if ($this->socid) {
127 $this->where .= ($this->where ? ' AND ' : '')."c.fk_soc = ".((int) $this->socid);
128 }
129 if ($this->userid > 0) {
130 $this->where .= ($this->where ? ' AND ' : '')."c.fk_user_creat = ".((int) $this->userid);
131 }
132
133 if ($categid) {
134 $this->where .= ($this->where ? ' AND ' : '').'EXISTS (SELECT rowid FROM '.$this->db->sanitize($this->categ_link).' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
135 }
136 // Add where from hooks
137 $parameters = array('socid' => $socid);
138 $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
139 $this->where .= $hookmanager->resPrint;
140 }
141
149 public function getNbByMonth($year, $format = 0)
150 {
151 global $user;
152
153 $sql = "SELECT date_format(c.date_creation, '%m') as dm, COUNT(*) as nb";
154 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
155 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
156 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
157 }
158 $sql .= $this->join;
159 $sql .= " WHERE c.date_creation BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
160 $sql .= ($this->where ? " AND ".$this->where : "");
161 $sql .= " GROUP BY dm";
162 $sql .= $this->db->order('dm', 'DESC');
163
164 $res = $this->_getNbByMonth($year, $sql, $format);
165 return $res;
166 }
167
173 public function getNbByYear()
174 {
175 global $user;
176
177 $sql = "SELECT date_format(c.date_creation, '%Y') as dm, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).")";
178 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
179 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
180 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
181 }
182 $sql .= $this->join;
183 $sql .= ($this->where ? " WHERE ".$this->where : "");
184 $sql .= " GROUP BY dm";
185 $sql .= $this->db->order('dm', 'DESC');
186
187 return $this->_getNbByYear($sql);
188 }
189
197 public function getAmountByMonth($year, $format = 0)
198 {
199 global $user;
200
201 $sql = "SELECT date_format(c.date_creation, '%m') as dm, SUM(c.".$this->db->sanitize($this->field).")";
202 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
203 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
204 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
205 }
206 $sql .= $this->join;
207 $sql .= " WHERE c.date_creation BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
208 $sql .= ($this->where ? " AND ".$this->where : "");
209 $sql .= " GROUP BY dm";
210 $sql .= $this->db->order('dm', 'DESC');
211
212 $res = $this->_getAmountByMonth($year, $sql, $format);
213 return $res;
214 }
215
222 public function getAverageByMonth($year)
223 {
224 global $user;
225
226 $sql = "SELECT date_format(c.date_creation, '%m') as dm, AVG(c.".$this->db->sanitize($this->field).")";
227 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
228 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
229 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
230 }
231 $sql .= $this->join;
232 $sql .= " WHERE c.date_creation BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
233 $sql .= ($this->where ? " AND ".$this->where : "");
234 $sql .= " GROUP BY dm";
235 $sql .= $this->db->order('dm', 'DESC');
236
237 return $this->_getAverageByMonth($year, $sql);
238 }
239
245 public function getAllByYear()
246 {
247 global $user;
248
249 $sql = "SELECT date_format(c.date_creation, '%Y') as year, COUNT(*) as nb";
250 //$sql .= ", SUM(c.".$this->db->sanitize($this->field).") as total, AVG(".$this->db->sanitize($this->field).") as avg";
251 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
252 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
253 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
254 }
255 $sql .= $this->join;
256 $sql .= ($this->where ? " WHERE ".$this->where : "");
257 $sql .= " GROUP BY year";
258 $sql .= $this->db->order('year', 'DESC');
259
260 return $this->_getAllByYear($sql);
261 }
262
270 public function getAllByProduct($year, $limit = 10)
271 {
272 global $user;
273
274 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->db->sanitize($this->field_line).") as total, AVG(tl.".$this->db->sanitize($this->field_line).") as avg";
275 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
276 $sql .= " INNER JOIN ".$this->db->sanitize($this->from_line, 0, 1, 1)." ON c.rowid = tl.fk_commande";
277 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
278 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
279 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
280 }
281 $sql .= $this->join;
282 $sql .= " WHERE ".$this->where;
283 $sql .= " AND c.date_creation BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
284 $sql .= " GROUP BY product.ref";
285 $sql .= $this->db->order('nb', 'DESC');
286 //$sql.= $this->db->plimit(20);
287
288 return $this->_getAllByProduct($sql, $limit);
289 }
290}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for MyObject.
Class to manage order statistics (customer and supplier)
getAllByYear()
Return nb, total and average.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
__construct($db, $socid, $mode, $userid=0, $categid=0)
Constructor.
getAverageByMonth($year)
Return the orders amount average by month for a year.
getNbByYear()
Return orders number per year.
getNbByMonth($year, $format=0)
Return orders number by month for a 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(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.