dolibarr 24.0.0-beta
expeditionstats.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-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
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
29include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
30include_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
31include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32
33
38{
42 public $table_element;
43
47 public $socid;
48
52 public $userid;
53
57 public $from;
58
62 public $join;
63
67 public $field;
68
72 public $where;
73
74
83 public function __construct($db, $socid, $mode, $userid = 0)
84 {
85 global $user, $conf;
86
87 $this->db = $db;
88
89 $this->socid = ($socid > 0 ? $socid : 0);
90 $this->userid = $userid;
91 $this->cachefilesuffix = $mode;
92
93 $object = new Expedition($this->db);
94 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
95 //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
96 $this->field = 'weight'; // Warning, unit of weight is NOT USED AND MUST BE
97 $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
98
99 //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
100 $this->where .= " AND c.entity = ".$conf->entity;
101
102 if ($this->socid) {
103 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
104 }
105 if ($this->userid > 0) {
106 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
107 }
108 }
109
117 public function getNbByMonth($year, $format = 0)
118 {
119 global $user;
120
121 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
122 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
123 if (!$user->hasRight('societe', 'client', 'voir')) {
124 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
125 }
126 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
127 $sql .= " AND ".$this->where;
128 $sql .= " GROUP BY dm";
129 $sql .= $this->db->order('dm', 'DESC');
130
131 $res = $this->_getNbByMonth($year, $sql, $format);
132 return $res;
133 }
134
140 public function getNbByYear()
141 {
142 global $user;
143
144 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).")";
145 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
146 if (!$user->hasRight('societe', 'client', 'voir')) {
147 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
148 }
149 $sql .= " WHERE ".$this->where;
150 $sql .= " GROUP BY dm";
151 $sql .= $this->db->order('dm', 'DESC');
152
153 return $this->_getNbByYear($sql);
154 }
155
163 public function getAmountByMonth($year, $format = 0)
164 {
165 global $user;
166
167 $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->db->sanitize($this->field).")";
168 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
169 if (!$user->hasRight('societe', 'client', 'voir')) {
170 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
171 }
172 $sql .= $this->join;
173 $sql .= " WHERE ".$this->where;
174 $sql .= " GROUP BY dm";
175 $sql .= $this->db->order('dm', 'DESC');
176
177 $res = $this->_getAmountByMonth($year, $sql, $format);
178 return $res;
179 }
180
187 public function getAverageByMonth($year)
188 {
189 global $user;
190
191 $sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->db->sanitize($this->field).")";
192 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
193 if (!$user->hasRight('societe', 'client', 'voir')) {
194 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
195 }
196 $sql .= $this->join;
197 $sql .= " WHERE ".$this->where;
198 $sql .= " GROUP BY dm";
199 $sql .= $this->db->order('dm', 'DESC');
200
201 return $this->_getAverageByMonth($year, $sql);
202 }
203
209 public function getAllByYear()
210 {
211 global $user;
212
213 $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).") as total, AVG(".$this->db->sanitize($this->field).") as avg";
214 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
215 if (!$user->hasRight('societe', 'client', 'voir')) {
216 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
217 }
218 $sql .= " WHERE ".$this->where;
219 $sql .= " GROUP BY year";
220 $sql .= $this->db->order('year', 'DESC');
221
222 return $this->_getAllByYear($sql);
223 }
224}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage shipment statistics.
getNbByMonth($year, $format=0)
Return shipment number by month for a year.
__construct($db, $socid, $mode, $userid=0)
Constructor.
getAverageByMonth($year)
Return the orders amount average by month for a year.
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
getNbByYear()
Return shipments number per year.
getAllByYear()
Return nb, total and average.
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.
_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: