dolibarr 24.0.0-beta
commandestats.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) 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.'/commande/class/commande.class.php';
31include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
32include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
33
34
38class CommandeStats extends Stats
39{
43 public $table_element;
44
48 public $socid;
49
53 public $userid;
54
58 public $from;
59
63 public $from_line;
64
68 public $field;
69
73 public $field_line;
74
78 public $categ_link;
79
83 public $where = '';
84
88 public $join;
89
90
101 public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
102 {
103 global $hookmanager;
104 $this->db = $db;
105
106 $this->socid = ($socid > 0 ? $socid : 0);
107 $this->userid = $userid;
108 $this->cachefilesuffix = $mode;
109 $this->join = '';
110 $object = null;
111
112 if ($mode == 'customer') {
113 $object = new Commande($this->db);
114 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
115 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
116 $this->field = 'total_ht';
117 $this->field_line = 'total_ht';
118 //$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
119 $this->categ_link = MAIN_DB_PREFIX.'categorie_societe';
120 } elseif ($mode == 'supplier') {
121 $object = new CommandeFournisseur($this->db);
122 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
123 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
124 $this->field = 'total_ht';
125 $this->field_line = 'total_ht';
126 //$this->where .= " c.fk_statut > 2"; // Only approved & ordered
127 $this->categ_link = MAIN_DB_PREFIX.'categorie_fournisseur';
128 }
129 //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
130 $this->where .= ($this->where ? ' AND ' : '').'c.entity IN ('.getEntity('commande').')';
131
132 if ($this->socid) {
133 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
134 }
135 if ($this->userid > 0) {
136 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
137 }
138
139 if ($typentid) {
140 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc';
141 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
142 }
143
144 if ($categid) {
145 $this->where .= ' AND EXISTS (SELECT rowid FROM '.$this->categ_link.' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
146 }
147 // Add where from hooks
148 $parameters = array('socid' => $socid);
149 $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
150 $this->where .= $hookmanager->resPrint;
151 }
152
160 public function getNbByMonth($year, $format = 0)
161 {
162 global $user;
163
164 $sql = "SELECT date_format(c.date_commande, '%m') as dm, COUNT(*) as nb";
165 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
166 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
167 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
168 }
169 $sql .= $this->join;
170 $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
171 $sql .= " AND ".$this->where;
172 $sql .= " GROUP BY dm";
173 $sql .= $this->db->order('dm', 'DESC');
174
175 $res = $this->_getNbByMonth($year, $sql, $format);
176 return $res;
177 }
178
184 public function getNbByYear()
185 {
186 global $user;
187
188 $sql = "SELECT date_format(c.date_commande, '%Y') as dm, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).")";
189 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
190 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
191 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
192 }
193 $sql .= $this->join;
194 $sql .= " WHERE ".$this->where;
195 $sql .= " GROUP BY dm";
196 $sql .= $this->db->order('dm', 'DESC');
197
198 return $this->_getNbByYear($sql);
199 }
200
208 public function getAmountByMonth($year, $format = 0)
209 {
210 global $user;
211
212 $sql = "SELECT date_format(c.date_commande, '%m') as dm, SUM(c.".$this->db->sanitize($this->field).")";
213 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
214 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
215 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
216 }
217 $sql .= $this->join;
218 $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
219 $sql .= " AND ".$this->where;
220 $sql .= " GROUP BY dm";
221 $sql .= $this->db->order('dm', 'DESC');
222
223 $res = $this->_getAmountByMonth($year, $sql, $format);
224 return $res;
225 }
226
233 public function getAverageByMonth($year)
234 {
235 global $user;
236
237 $sql = "SELECT date_format(c.date_commande, '%m') as dm, AVG(c.".$this->db->sanitize($this->field).")";
238 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
239 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
240 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
241 }
242 $sql .= $this->join;
243 $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
244 $sql .= " AND ".$this->where;
245 $sql .= " GROUP BY dm";
246 $sql .= $this->db->order('dm', 'DESC');
247
248 return $this->_getAverageByMonth($year, $sql);
249 }
250
256 public function getAllByYear()
257 {
258 global $user;
259
260 $sql = "SELECT date_format(c.date_commande, '%Y') as year, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).") as total, AVG(".$this->db->sanitize($this->field).") as avg";
261 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
262 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
263 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
264 }
265 $sql .= $this->join;
266 $sql .= " WHERE ".$this->where;
267 $sql .= " GROUP BY year";
268 $sql .= $this->db->order('year', 'DESC');
269
270 return $this->_getAllByYear($sql);
271 }
272
280 public function getAllByProduct($year, $limit = 10)
281 {
282 global $user;
283
284 $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";
285 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
286 $sql .= " INNER JOIN ".$this->db->sanitize($this->from_line, 0, 1, 1)." ON c.rowid = tl.fk_commande";
287 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
288 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
289 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
290 }
291 $sql .= $this->join;
292 $sql .= " WHERE ".$this->where;
293 $sql .= " AND c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
294 $sql .= " GROUP BY product.ref";
295 $sql .= $this->db->order('nb', 'DESC');
296 //$sql.= $this->db->plimit(20);
297
298 return $this->_getAllByProduct($sql, $limit);
299 }
300}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage predefined suppliers products.
Class to manage customers orders.
Class to manage order statistics (customer and supplier)
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, $typentid=0, $categid=0)
Constructor.
getNbByYear()
Return orders number per year.
getAllByYear()
Return nb, total and average.
getAverageByMonth($year)
Return the orders amount average by month for a 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
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.